Many people have noticed that after a rain, worms appear in gardens, on sidewalks, and scattered across neighborhood lawns, much to the delight of local birds. Modern humans are far from the first to make this observation: authors dating back to the Renaissance have noted the appearance of worms after a rain. There are several interconnecting factors to explain why they come out when it rains, but the short story is that worms find conditions above ground to be especially favorable then.
A common myth about worms is that they are forced to come out of their holes during the rain because they would drown if they stayed below. In fact, this is not true. Worms breathe through gas exchange, absorbing oxygen directly through their skins. As long as water has sufficient dissolved oxygen, worms can actually live for several days fully immersed in water, as scientists have discovered.
The way in which worms breathe does explain why they tend to congregate above ground after the rain, however. Worms are covered in a mucus that facilitates gas exchange, and as a result, they must stay moist. Most of the time, conditions above ground are too dry and hot for worms, and they will dry out and die because they cannot breathe. After a rain, the environment is moist, facilitating breathing and also making it easier to crawl along the ground, so worms are encouraged to surface.
As for why worms come out above ground at all, worms prefer to mate above ground. They often come out after a rain in the hopes of finding mates, treating the above-ground world like a sort of worm discotheque, with a wide sampling of potential mates available. Studious observers may have noted that worms often congregate in small groups above ground, illustrating their primary reason for surfacing. Worms, incidentally, are simultaneous hermaphrodites, so both partners exchange sperm, which is used to fertilize eggs.
In addition to surfacing after a rain, worms come out at night as well. During the evening hours, the air is typically much cooler, and the environment is often moist, making it hospitable to worms. People who use worms for fishing bait often choose the evening hours to go hunting for “nightcrawlers,” taking advantage of this trait to collect worms for fishing trips.