Having worms in your garden can be very good for your soil. Worms fertilize the soil by eating organic matter in the soil and excreting castings that are rich in nitrogen, phosphorus, and potassium. These are the key minerals needed for plant growth, and having them available in soil can help plants thrive. Earthworms leave behind excrement or castings containing from 5 to 11 times the amount of nitrogen, phosphorus, and potassium they have ingested by concentrating the organic constituents in their food and making it more available to plants. Earthworm castings also help bind key minerals such as calcium, iron, and sulfur to soil particles. Further, when earthworms die, their bodies decompose quickly, adding nitrogen and other nutrients to the soil.
Earthworms can also help create good soil structure. When they burrow through the soil, they create space for air to reach plant roots, helping plants get oxygen in their roots. Their burrows can also break up hard soil, allowing plant roots to reach deeper. They also create better drainage by making channels in the soil; soils with earthworms can drain up to 10 times faster than soils without earthworms.
Earthworms can be very beneficial to crop production; one study found that on average, earthworm presence increases crop yield by 25% and aboveground biomass by 23%, while not affecting the quality of the crops.
Sources: