Earthworms have a large cavity called the coelom, which runs through the length of the whole body.
This cavity is filled with coelomic fluid which is what worms secrete. The fluid is milky white in colour and is thick and gelatinous depending on conditions.
It is especially secreted in times of stress, to prevent itself from drying up in dry conditions, and to protect itself from predators.