In its crystal form bone dry, sucrose (C12H22O11) loves to bind with water (H20). When present in sufficient concentrations, table sugar will suck all of the water around it. This is why sugar is an excellent food preservative. Via osmosis, the sugar pulls the available water from within the foodstuff, reducing the food's aw, thus making it unsuitable for microbes to grow, or even survive.
More specifically, at the outer edge of a cell is its membrane, a semi-permeable barrier that allows some substances, including nutrients and wastes, to move in and out. With a higher concentration of sugar outside the cell, the solution is hypertonic, meaning it will draw water from the cell, causing the bacteria (or whatever cell) to shrivel and die. (The reverse could potentially happen as well if the sugar concentration was higher inside the cell, hypotonic, with it drawing water in, perhaps to the point of bursting the cell.)
On a chemical level, it's pretty interesting as well. Notice all the hydrogen and oxygen involved; between the two molecules, there are 24 hydrogen atoms and 12 oxygen. Each oxygen atom has a slight negative charge and each hydrogen atom has a slight positive charge, and in chemistry, opposites attract. Together, all of these hydrogen and oxygen atoms pull at each other – initially to form their respective molecules (table sugar or water), and then in the process that kills the microbe.
You can also observe this absorption effect simply by taking some cotton candy, which is made of pure spun sugar, and placing it in a humid environment. With just 33% relative humidity, cotton candy left out in the air will completely collapse and crystallize in just 3 days as it absorbs the moisture in the air. At 45% relative humidity, it will completely collapse in just one day. At 75% humidity, it takes just 1 hour. This is why it has only been since 1972 that non-"made on demand" cotton candy has been available. (1972 was when the first fully automated cotton candy machine was invented that could make the fluffy treat and quickly package it in water tight containers).
source: gizmodo.com by Melissa @ TodayIFoundOut.com