Earthworms consume decaying vegetation and small particles of organic matter from the soil. Their primary diet is made up of leaf litter and other cast-off pieces of plants. Although they are important decomposers, they generally do not eat rotten meat or dairy.
Earthworms swallow soil, but they are only able to digest the parts of the soil that contain organic material. They cannot directly digest minerals like iron or nitrogen that may be present in the soil. They excrete these minerals, along with whatever is leftover from the plant matter they did digest. Since the plant material is broken down within their feces, the compounds within it are easier for plants to take up with their roots. In this way, important minerals within the soil are moved about and the soil overall is enriched with their feces.