"If I Forget Thee, O Earth . . ." by Robert Heinlein is a coming-of-age story in which a father in an isolated Moon colony takes his son to see the planet Earth, which has been ravaged by a nuclear holocaust, for the first time. The surface-level answer to your question is that the Moon colony survived because its inhabitants were safely away from Earth during the devastation. As the story makes clear, though, it's not that simple. Once supply ships no longer come from Earth with provisions, the colony has to learn the difficult lessons of how to survive on its own.
Then had followed the years of despair, and the long-drawn battle for survival in this fierce and hostile world. That battle had been won, though barely: this little oasis of life was safe against the worst that nature could do.
However, even the battle for physical survival doesn't tell the whole story. The pilgrimage that Marvin and his father make is to ensure the continuing moral and spiritual survival of the colony's inhabitants, to give them a vision of something for which to strive.
But unless there was a goal, a future towards which it could work, the colony would lose the will to live, and neither machines nor skill nor science could save it then.
So Heinlein makes clear that there are three facets to the colony's survival of the nuclear holocaust that destroyed Earth: its physical presence on the Moon where it is safe from the radiation, its continual struggle against the harsh natural conditions on the Moon, and the vision that the original colonists impart to their children of someday returning to Earth, their homeland.