Dogs and puppies live through their noses, and as many dog owners can tell you, pungent scents can prompt rolling behavior. Think of it as a scent ecstasy, similar to what cats experience when exposed to catnip. When a dog finds what he considers an attractive odor, he rolls to rub his shoulders, back, and neck into the offering.
We don't know for sure why dogs are drawn to roll around in things that smell repugnant to humans. But there are a few possible explanations.
Dogs Have a Nuanced Sense of Smell
It's no secret that dog noses are much more sensitive than those of humans. But it's not just that a dog's sense of smell is more acute than ours, dogs can actually detect more layers of scent.
For instance, when a skunk sprays a rose bush, a human only smells the skunk's aftermath, as it's a more recent and stronger odor than that of the rose bush. But if a dog sniffed the same rose bush, it would be able to smell both the skunk's and the rose's, and probably myriad other odors as well.
This is likely an evolutionary trait that helped dogs communicate with their packs when wild canines roamed the earth. "Perfuming" themselves with such scents may allow dogs to carry the smelly message home, so other dogs in the pack can learn all about a potential food source.
Since many species of wild dogs were scavengers, they'd be drawn to smells such as rotting carcasses (which, not to be too graphic, smell not unlike feces).
Dogs May be Trying to Mark Their Territory
It's well known that pack and territorial animals will mark their territory by urinating on it. This scent lets other rival packs know to avoid a given area unless they want to fight for it. A wild dog or wolf rolling in poop (or other animal matter) may be trying to override another animal's scent, or intentionally leaving its own scent as a warning.
Again, not the clearest explanation for why pet dogs engage in this behavior, but it at least provides some evolutionary clues.
Dogs Rolling in Poop May be Using Camouflage
Like their predecessors, pet dogs may roll in poop and other unpleasant-smelling material in order to mask their own scent. Wolves, especially, would not want a potential prey animal such as a deer to be able to smell them coming. If its own smell was camouflaged with the smell of poop, the predator would have an easier time hunting its prey.
Sometimes, Dogs Just Get Bored
It's also well-established that a bored or understimulated puppy will engage in destructive behaviors like chewing and digging. So it's not too much of a stretch to think that rolling in poop may be a sign that you need to pay more attention to your dog and keep him occupied with other, less smelly hobbies.