Just being a dog- I guess it masks the dog's scent but really I think they just like to get all smelly. I did have a beagle named Pink and she loved to roll on bath soap wrappers which was kind of nice. Better to smell like Ivory than dead snake.
I read that they do it so they can show other pack members that they are well traveled, err, something to that effect.
This article says they also use it as camouflage, which is the theory I believe in most. My dog Tuki loves to roll in something, I think it's dead or dying earthworm or something else that stinks to high heaven, and she is also very fond of stalking type play, so maybe those old hunting traits are being expressed.
Reminds me of a story...I live next to a pretty good
size lake. There is a lake patrol guy that patrols around
the lake a couple times a day.
He had this female GSD/Golden retriever mix that
always stood up in the front of the boat as he was
patrolling. One day during his normal patrol he stopped
on the shore next to a wooded area to hunt some mushrooms.
He let the dog run around and do whatever she wanted.
When he got ready to head home the dog was nowhere
to be found. He called and called and searched for her.
Found her about a 1/2 hour later next to the shoreline
rolling on a dead deer carcass.
He finally got her in the boat and headed off across the
lake towards home. He only got about 1/2 way before
the smell got to be too much. So he dumped her into
the water and made her swim home. He said by the
time she made it to shore she was at least tolerable...