Abraham pleads to the Lord on Sodom's behalf, saying "you wouldn't treat the righteous and wicked alike, would you?" God is a God of Justice as much as he is of Compassion. And in this situation he chose compassion, for he treats the righteous and the wicked the same by saying he would spare them all because of a few good people. but in the end, He doesn't treat the righteous and the unrighteous the same, He spares the four who would listen, and destroys the rest.

Genesis 18:23-25