Why do earthworms come out after it rains?
The old answer, that they would drown underground, is wrong. The real answer is that rain opens a window for a worm to travel a long way without dying of dryness.

After a wet night the pavement is covered in earthworms, and the explanation everyone learned at school is that they surface to avoid drowning. It is almost certainly wrong. Earthworms breathe through their skin and need it moist to do so, but they can survive submerged for days in oxygenated water. The better-supported explanations are about movement, predators and breathing, in that order.
They are travelling, and rain is the only time they can
A worm on dry ground loses water through its skin quickly and dies within hours. On wet ground it can cross a garden, a road or a field in a night. The leading explanation among soil biologists, including Chris Lowe of the University of Central Lancashire whose reasoning is widely cited, is that rain gives earthworms a rare chance to migrate and find mates without desiccating. Surface travel is far faster than burrowing, and a wet night is the window. The worms you see on the pavement at dawn were mid-journey when the sun came up.
Vibrations that sound like a mole
Moles hunt earthworms, and worms flee upwards from the vibrations of a mole digging. Raindrops drumming on the ground produce similar vibrations. This is not just a hypothesis: worm charmers win competitions by driving stakes into the ground and rubbing them to imitate a mole, and the worms rise. Some of the post-rain surfacing is probably false alarms of this kind.
Breathing, with a twist
Worms take in oxygen through moist skin. In waterlogged soil the water is often low in oxygen, especially in compacted or clay soils, and a worm can gain oxygen by moving up into puddles or onto the surface where the water film is thin and well aerated. So the school explanation contains a grain of truth: worms leave low-oxygen water, not water as such. The ones you find dead in puddles usually died of ultraviolet light and drying once the rain stopped, not of drowning.
Why it only happens after certain kinds of rain
Worms surface most after long, gentle rain that soaks the soil, and at night, when ultraviolet light is not a hazard. A short violent downpour on dry ground barely wets the top layer and produces few worms. Warm nights in spring and autumn, with the soil moist and above about 10 °C, are peak worm weather; frozen or baked soil keeps them deep.
What it means for your garden
A lawn that produces a lot of worms after rain is a healthy one; they aerate and fertilise it. If you want to walk without stepping on them, wait until an hour after sunrise on a wet morning and they will have gone back down. If you want to know when the next soaking rain arrives, the city forecast shows rain hour by hour from five models, and the radar shows the band that is already on its way.