Because soils and subsoils are critical components of any larger ecosystem, and because soils themselves constitute small ecosystems, worms play a very significant role in all ecosystems they inhabit. Worms affect the mix of macroorganisms and microorganisms that inhabit the soil itself, and in turn affect plant growth as it relates to this biotic community. This in turn affects the animal life above ground that is imbricated into the plant community. Worms also alter the actual structure of soils, and so affect the hydrological conditions of the soil and the ability of plants to penetrate the soil with their roots.
Earthworms borrow; that is, they force their way through soil and subsoil, creating tubular spaces in their wakes. Epigeic earthworms burrow through the topsoil, loosening it to allow more air and water. Anecic worms create long tunnels that reach from the organic layer into the mineral layer, some as deep as 6 feet. Endogenic worms create networks of tunnels that lace laterally along the margin between mineral and organic layer. Not only does the burrowing create access for water, air and plant roots, but the worms themselves transfer organic matter and minerals back and forth between the soil layers, bringing minerals up from below and depositing organic matter more deeply.
Earthworm bodies emit three substances: body slime, castings and urine. The body slime is left behind in burrows, where it encourages the growth of nitrogen-fixing bacteria, which increase soil fertility. The urine is nitrogen-rich, too. The worm castings -- solid waste -- are a mixture of digested food and ingested grit from the soil; and the castings are pH neutral, so worms can actually correct overly acidic soil over time. The castings themselves contain four essential nutrients for plants: nitrogen, phosphorus, potassium and calcium.
Epigeic worms are the ones people see most often -- the worms that live near the surface. They feed at night, being photophobic. Their burrows are not very deep, and during hard rainfalls, the worms are forced upward to avoid drowning. They seek refuge in areas with the least light, as a survival mechanism, because when forced into the open, they become prey for many species of birds. Anyone who observes robins -- enthusiastic worm predators -- will see the robins on hand before the rain even stops falling. So worms participate in the ecosystem this way, too.