For more information, go here.
Earthworms do not have lungs, but exchange oxygen and carbon dioxide through their
skin by
diffusion.
To successfully diffuse gases through the skin, the earthworm must remain moist. Damp or moist soil helps the earthworm retain moisture, as does the mucous produced by the epidermis of the earthworm.
Earthworms usually surface at night when the air is likely to be cool and moist. Earthworms have no eyes to detect light, but tissue near the head is sensitive to light enabling the earthworm to detect light stay safely below ground during the day when they could be affected by the sun.