Earthworm behavior is simple, and most every aspect is understood with the exception of coming to the surface following a rain. Most of earthworm behavior consists of moving through the soil for food and reproduction.
Earthworms move by contracting the muscles in their segments in a wave motion. This makes the body shorten and lengthen, pushing the earthworm ahead with each cycle. While the earthworm is shortened, it extends small bristles called setae, anchoring it in position. The anterior of the earthworm remains unanchored, so when it extends, the front portion moves ahead, after which, the setae contract so the posterior portion of the body can move ahead, also. Earthworm locomotion is aided by the secretion of lubricating mucus that can help soften the soil and help the earthworm slide through it.
As earthworms move through the soil, they push air through it. This has prompted some scientists to call them biological pistons. As the soil is aerated and the earthworms deposit their excretions in it, the soil is made more fertile. Some earthworms actually move near the surface, where nutrients are more abundant, to feed and then, move back down to process the food, pulling nutrients further down into the soil. Many gardeners introduce earthworms to their soil because of this. In fact, Charles Darwin theorizes in one of his books that no other animal has been so important to world history than the earthworm.