Worms don't have lungs like us, so they take a different approach to breathing underground. They absorb oxygen directly through their moist skin. Their skin is covered in a special mucus that helps them with this process. This is why worms need to stay damp - if they dry out, they can't breathe!
This explains why you see earthworms on the surface after a heavy rain. Contrary to popular belief, they're not coming up for air because the soil is flooded. Instead, heavy rain can temporarily fill the air pockets in the soil with water, making it harder for worms to breathe through their skin. So, they come to the surface to find a place where they can breathe more easily.