Almost indefinitely, according to science
I've lived my life under the assumption that all food, even the shelf-stable pantry staples that you buy once and let sit in your cabinet for years, eventually spoils. But that's not always true, especially when it comes to the shelf life of sugar. According to Domino Sugar, "Sugar, properly stored (tightly closed and in a dry place), has an indefinite shelf life." That doesn't seem possible, but it's true, not just some manufacturer's promise. "Technically sugar never spoils," explain food safety experts from the US Department of Agriculture's Food Service and Inspection Service, though they do add, "for best quality it is recommended to use within two years of opening."
But how is that even possible? Why does sugar never spoil, even though white, granulated sugar seems like it would be a perfect breeding ground for bugs, mold, and bacteria?
It turns out that like salt, sugar prevents microbial growth. That's part of the reason jams and jellies have such high sugar content; it's an effective way to preserve the fruit. As Mickey Parish, chair of the Nutrition and Food Science Department at the University of Maryland, writes in Scientific American, sugar prevents bacteria from growing because of osmosis, or dehydration. "This has the effect of drawing available water from within the food to the outside and inserting salt or sugar molecules into the food interior," he explains.
That tendency for sugar to dehydrate means that there just isn't enough water for most microbial colonies to thrive in high-sugar environments—like the bag of sugar sitting on your pantry shelf.
But the quality of sugar can still deteriorate if you don't store it properly. Sugar absorbs smells easily. And just because sugar isn't going to grow bacteria doesn't mean it's not susceptible to bugs (and believe me when I say there's nothing more skin-crawling than worrying if you're accidentally eating ants in your pancakes). That's why you want to store sugar in an opaque, airtight, and moisture-proof container on a cool, dark shelf in your kitchen.
If you see lumps in your sugar, that doesn't mean the sugar has gone bad. It just means that it's been exposed to a bit of moisture. All you have to do to use that sugar is break up the lumps, and take out a scoop, and never worry about sugar spoiling again.