Salt and sugar both kill microbes by causing water to exit through their cell membrane, causing dehydration. This phenomenon is a quintessential example of the intersection of physics, chemistry, biology, and "everyday life".
Notice how nature likes to remain "in balance". For example, balloons deflate over time because the air pressure inside them is greater than the air pressure outside. When air travels through a tiny opening in the balloon, this pressure differential is reduced, eventually bringing the pressure inside the balloon down to atmospheric pressure. Balance has been restored. In the physical sciences, this idea is know as Le Chaltlier's principle and stated as follows. "When stress is applied to a system at equilibrium, the system will shift in the direction which relieves that stress."
We can think of a microbe as a sack of fluid. That sack (the cell membrane) has tiny openings where very small molecules can exit, much as air could exit from the balloon in the previous example. Inside the cell membrane are the "guts" of the microbe, what microbiologists call "organelles". Just like our organs are surrounded by blood, these organelles are floating in a fluid containing just the right balance of chemicals to support life. The ratio of dissolved solids to water is called the "osmolality" of the fluid, and maintaining this ratio is critical to the microbe's existence. (Osmolality has specific units and a somewhat more complex definition, but the definition above will suffice for this explanation.)
Now suppose the microbe is surrounded by salt, sugar, or both. Outside the cell membrane, there is a solid with little to no water, and inside the cell membrane is a solution with a relatively high water to dissolved solid ratio. LeChatlier's principle dictates that balance must be restored. Nature will not accept the discrepancy between the water/solid ratio inside the cell and the water/solid ratio outside.
In a futile attempt to equalize the two ratios, the cell membrane begins expelling water. However, because there is so much salt/sugar outside the cell, the microbe must expel nearly all of its water. The result is death by dehydration. Larger organisms, such as insects, can be killed in a similar fashion as each of their cells undergoes this process. However, because insects have many cells (while microbes have only one), it is possible that the insect may escape before dehydrating.
By killing potentially harmful microbes before they can grow or produce toxins, salt and sugar prevent food spoilage, allowing foods to be stored for long periods of time, often without refrigeration.