It is not fully understood why earthworms come up to the surface when it rains, though there are a few theories. The theory with the most evidence is that earthworms find it easier to move across the soil surface when it is wet, to find food, new habitats or a mate.
When the rain hits the ground it creates vibrations on the soil surface. This causes earthworms to come out of their burrows to the surface. Earthworms find it easier to travel across the surface of the soil when it is wet, as they need a moist environment to survive.
Birds exploit this behaviour by mimicking rain hitting the ground by drumming their feet to encourage the earthworms to the surface to feed on them.