Earthworms have no teeth. That doesn't stop them from eating just about any dead organic matter they come across. They also swallow soil as they burrow. The microscopic animals in each bite of soil become worm food, too. An earthworm's gut is a great place for bacteria to grow.
Earthworm castings add more bacteria back to the soil than the worm eats.
More bacteria mean healthier soil. All this adds to the humus in the soil. It's a myth that earthworms come to the surface when it rains because they are drowning. Some scientists think that this behavior helps worms migrate to new territory or to find a mate. But no one knows for sure why they do it.