Worms do not like soil that is too hot or too cold. They generally inhabit soil that is slightly damp and cool and which may be located under leaves, fallen logs or rocks. Moist soil helps worms to breathe more easily. Earthworms do not tend to like hotter summer months when temperatures rise above 75 degrees Fahrenheit.
Some worms live in the top layers of the soil near layers of decaying leaves. Other species of earthworms dig burrows deep into the soil. Those worms that inhabit the deeper layers of the soil must rise to the surface when it rains to avoid drowning. They also come to the surface to feed on decaying leaves and plants.
When soil conditions become too hot, earthworms become less active. Earthworms cannot survive when temperatures become too cold and will often burrow more deeply into the soil where conditions are less extreme. Some species of earthworms are able to roll themselves into balls and excrete protective mucous during extreme conditions. Body functions slow down in this state while worms wait for the environment to become more hospitable.
Most worms will spend most of their lives in the topsoil and will rarely move more deeply into the soil than 9 inches. Some worms live in the mud in streams or along the shores of swamps. Worms can also live in decaying plant matter. They can also live in animal manure from which they can derive nutrition.