Years ago, cars were a luxury and most officers had to walk a beat. Midnight shift got pretty lonely, and you got cold and hungry. You couldn't always go back to the station, as you had to stay in your beat.
There were no 24 hour convenience stores back then, everything closed at night. The first places open in the morning were the coffee shops, which also served donuts.
This was the first chance for the officer to warm up, and have a donut and cup of coffee to give him some energy for the rest of his shift. Those fortunate to have cars would often meet the beat guys at the coffee shops to share stories.
I find it humorous that people make donut jokes thinking it is offensive to us. If an officer does take offense, then he needs to have a lesson in tradition.
As you can see, there is some truth to it (Badge 203 was close in his answer, but they didn't even have microwaves back when this all started).
Law enforcement since 1991