In my garden when I turn a over a rock, lift a brick or a pot plant, especially in summer, I'll find a worm. They like cool, moist, dark places. So you'll find them in soil that is rich and damp and covered with leaf litter or mulch.
In cold weather, if you search in the soil you will find mature and young earthworms as well as eggs. They become active when the soil starts to warm in the spring. By late spring, most worms are mature. As temperatures rise and the ground becomes dry, activity slows and they move deeper below the ground. Many lay eggs and then die, so by midsummer those worms remaining are very young or protected by egg capsules. As the weather cools, young worms emerge. With the onset of wet weather, they grow active, making burrows and eating extra food, resulting in more worm castings. Egg laying occurs again. Activity continues as long as the soil is damp.
From a gardener's perspective, the earthworm's life task is to break down organic matter and add it back as nutrient-rich pH neutral worm castings (that's poo, a soil conditioner called vermicast). An earthworm produces its weight in castings daily.
Leading with its pointy head, and using its bristle-like hairs, called 'setae', it pushes through the soft earth, swallows soil or organic matter, grinds this in its gizzard and excretes tiny castings to line the burrow or deposit at the entrance hole. Deep-burrowing worms aerate the soil and condition it by digging deep, long tunnels, bringing oxygen to roots of plants. Composting worms are surface dwellers who live under leaf litter.
The application of chemical fertilizers, sprays and dusts can be disastrous for worms. Nitrogenous fertilizers tend to create acidic conditions, which are fatal for worms. In Australia, changes in farming practices such as the application of super phosphate to pastures had a devastating effect on the Giant Gippsland earthworm, which is now a protected species.
After rain, earthworms often appear above the ground. They haven't drowned, as fresh water doesn't disturb them. However, stagnant or contaminated water will force them from their burrows. Earthworms can survive in soil that freezes gradually.
So to encourage worms, keep the soil moist, with plenty of leaf matter and avoid artificial fertilizers.
Adding compost to the soil is an excellent way of attracting worms, as well as increasing moisture.