Question as stated. I haven’t seen anything to suggest it was punishment for an offence (he didn’t seem to have ever committed any) or that he was only useful for that particular assignment (he was one of the best), so why?
Out of universe
If you look at 20th century military, especially conscripted ones which is what First Order had - for example Soviet Army, "painting fences", "digging latrines", "cultivating potatoes" was a standard duty for soldiers.
According to the "Before the Awakening prequel book, the timeline was the following:
What that means is that Finn was on the Starkiller not even as Stormtrooper, but as a cadet in training.
So assigning cadets in training to Sanitation seems logical for out-of-universe reasoning above - they are the most useless of soldiers around yet.
Here's how training for cadets is described in the prequel:
Another theory (UPDATE!!!)
It just occurs to me - the "I was in sanitation" line given to Han was the only actual canon evidence of "Sanitation" job. But was it the truth"?!?!
Finn is always trying to create a positive impression of himself with the Resistance, from the get-go:
As such, I am pretty sure telling Han "Hey, I was 1% of my class, model stormtrooper, groomed for promotion by Captain Phasma" isn't high on his list of brand management plans. Far better to pretend to be a lowly "sanitation" worker than top-flight cadet that he really was.
I asked a separate question to verify if we have any proof he was telling the truth: Is there any canon evidence that FN-2187 actually worked in sanitation?
If you look at the only Star Wars universe garbage disposal facility that has actually been portrayed on screen, in episode IV, it should be obvious why you need trained, armed and armoured soldiers working in your sanitation section.
He was in sanitation for two reasons.
Basically, he works in sanitation because it adds humor to the story.
Although @DVK 's answer is much better than mine, I'll still add my €0,02. Disclaimer: I haven't read the book, in which both Phasma's and Finn's characters are apparently fleshed out a bit more.
I would interpret it as Finn using as a euphemism for being part of a stormtrooper force who basically exterminated an entire village, the same way that Tony Soprano claimed to be in 'waste management' shortly after killing somebody. In this context, 'sanitation' would actually refer to the janitorial duties he had, instead of 'sanitizing' a village, but still it would not make any sense for him to reveal that he was a stormtrooper at that moment.