The slime is secreted to provide a barrier between the slug's extremely fragile body, and the rough surface of the ground over which it travels. The slime is such a good protection to the slug, that it can crawl over the sharpest of rocks and stones without damage to its skin.
All slugs really are, are snails without shells... snails excrete slime too, and in fact, leave slime trails when on a smooth, dry surface like terrarium glass... it ALSO acts as a barrier against disease. Slugs don't have lungs, they breathe through their skin... the mucous allows waste to diffuse out of their bodies, and prevents anything but oxygen and nutrients they need from soil from getting IN.