Googling, I find two different explanations for why earthworms tend to surface after a rain.
This page from UCSB, for example, gives both answers, contradicting itself:
The first explanation is that worms can breathe fine when it's wet underground, but surface after rain because it's their only chance to surface without drying out:
Worms, like all other animals need to breathe, but since worms do not have mouths, they have a different way they breathe. Worms breathe directly through their skin, but they can only do so if their skin remains moist. Deep down in the soil, where worms typically live, it is normally moister than the surface. For this reason, worms prefer to live underground. After it rains, the surface becomes wet enough to support worm life, so they can come above ground to explore new areas more quickly. Fun fact: worms actually don’t come above ground after it rains to avoid drowning. Earthworms can live underwater for approximately two weeks. Thank you very much for your question!
The second answer is that worms drown if they stay under ground after a rain
Worms are animals and, like you and me, they require oxygen to live. Instead of breathing air though their mouth into lungs, worms absorb oxygen from the air across their skin. Worms eat dirt. They burrow into the soil and normally live underground, where they are protected from the sun and so won't dry out. If you've ever played in the dirt by digging holes, you probably noticed that there are small holes or tiny pockets of air. The worms use that air, and the air that enters through their burrow holes, to breathe. When it rains and the dirt absorbs a lot of water, those air holes disappear, and the worms' burrows fill with water. If the worms do not come to the surface during a heavy rain, they will suffocate. Likewise, if they do not return to the soil after the sun comes out, they will dry out and die.
Which of these two is correct?