The Rambam writes in Hil Teshuvah 3:2
אדם שעונותיו מרובין על זכיותיו מיד הוא מת ברשעו שנאמר על רוב עונך. וכן מדינה שעונותיה מרובין מיד היא אובדת שנאמר זעקת סדום ועמורה כי רבה וגו'. וכן כל העולם כולו אם היו עונותיהם מרובין מזכיותיהן מיד הן נשחתין שנאמר וירא ה' כי רבה רעת האדם.
A person, city, or country whose sins outweigh its merits is immediately sentenced to death and destruction; a person, city, or country that has a majority of merits gets to keep going.
Lechem Mishna asks: The Rambam proves that this calculus applies on the broader level to the city or country, not just to the individual, from the fact that G-d destroyed Sdom. But if that is true, then why did Avraham bother to daven on their behalf? Either the numbers add up to their being spared or they don't?!
Two weeks ago R' Eliezer Eisenberg discussed a similar question. If the calculus applies even to the entire world, as the Rambam writes, then why was Noach spared during the flood? Why wasn't the entire world destroyed? See here.
It's a great kasha, but the answer is even better. Lechem Mishna suggests that tefilah overrides the calculus. Mipnei tefilas Avraham ha'ya mochel af al pi she'ain ha'din kach. Some of you I am sure are thinking that what he means is that the tefilah itself is a zechus that comes in and tips the scales. Maybe tefilah creates a tziruf between the one who davens and the city and his merits now get counted for them. A nice sevara, but that's not what the Lch"M is saying. That sevara would mean the city legitimately deserves to get off. What the Lch"M is saying is that the scales are still tipped in the direction of destruction -- the city does NOT deserve to be spared. Nonetheless, G-d is willing to spare it anyway. There is, so to speak, an override button. (Does that mean the judgment is not true? I would say see Mei HaShiloach in P VaYeishev end of the first piece on the difference between emes and emes l'amito. G-d's justice is emes l'amito, not just emes.) The power of tefilah is so great that G-d puts aside what justice demands.
So we see the issue raised in Noach, we see raised in connection with Sdom. One more place: Lot begs the angels for permission to flee to the city of Tzo'ar to seek refuge there, meaning they would have to spare the city. Or haChaim (19:20) asks: if the Tzo'ar deserved to be destroyed because its sins outweighed its merits, then why should it be spared just because Lot wanted to take refuge there? And if it's sins did not outweigh its merits, then shouldn't it be spared even if Lot didn't take refuge there? Take a look at his answer as well as Shem m'Shmuel (5678).
Now for the flipside. G-d says that the cities of Sdom and Amora will be destroyed because, "Za'akas Sdom v'Amora ki rabah, v'chatsam ki kavdah me'od." (18:20) The Netziv and Brisker Rav note the redundancy. If "chatasam" seals their fate, why add the beginning of the pasuk about "za'akas Sdom v'Amora?"
Netziv quotes Tos B"K 93a d"h echad that when the poor cry out in oppression, G-d responds much more quickly than when there is no one crying out. Ramban writes that "za'akas Sdom v'Amora" refers to interpersonal crime. The people of Sdom sinned against each other, not just against G-d. If a person commits idolatry, it is essentially a victimless crime. Not so when a person commits theft, arayos, and other such evils. Sdom was filled with the cries of victims.
The Lch"M taught us that if you, or even someone else, cries out in tefilah to G-d, that averts din. The Netziv is teaching us that if the victim of sin cries out to G-d, that hastens din.
It's an amazing thing -- G-d kavyachol us moved by the pleas of we puny human beings. Our words matter more than we can imagine.