Earthworms breathe through their skin in a process called cutaneous respiration. Their skin is permeable to gases, allowing oxygen to diffuse directly into their bloodstream from the surrounding environment. Similarly, carbon dioxide, a waste product of metabolism, diffuses out through their skin into the surrounding environment.
To facilitate this exchange of gases, earthworms require a moist environment. Moisture helps keep their skin moist and allows gases to dissolve, making it easier for oxygen and carbon dioxide to pass through. This is why earthworms are typically found in damp soil or other moist environments. If their skin dries out, they can't effectively exchange gases and may suffocate.