It is from Allah’s Sunnah that whoever does not flee from Allah’s punishment eventually ends up in it. This is why Allah commanded Nuh (peace be upon him) to board the ship with whoever believed, and commanded Lut (peace be upon him) to exit from the midst of his people, rather commanding him not to turn back to even look at them.
Moreover, if punishment was to afflict a town, and the believers were to be saved from it, the truth would become distinct from falsehood before it [the punishment] even arrives, and then this world would no longer be a world of trial. Rather, Allah only wanted the purification and distinction in this world to be an amount that does not necessitate removing taklif (human responsibility) and Iman in the unseen. So purification and distinction is continuous, and it is among the most important objectives for fighting battles, except that it comes in the tail-end of the means.
[Faydh al-Bari `ala Sahih al-Bukhari, Muhammad Anwar al-Kashmiri (4/387)]
من لم يهرب من عذاب الله يقع فيه