Landing
Sugar water has a skin-like surface tension. Insects are light enough that they can land on this watery film without sinking, and they can then fly away unharmed.
Surface Tension
Water molecules are composed of two hydrogen atoms and one oxygen atom. The surface tension of water is caused by a process known as hydrogen bonding, or what happens when the positively charged hydrogen atoms in each water molecule is attracted to the negatively charged oxygen atoms of other water molecules.
Detergent
Detergent is a surfactant, a substance that disturbs or breaks the hydrogen bonds between water molecules.
Broken Bonds
With the hydrogen bonds broken by detergent, water loses its surface tension. Insects can no longer land on the water's surface, and will sink into the liquid instead.
Fun Fact
Sugar water and detergent traps are also used by entomologists to catch and count bees for insect population surveys.