As indicated by their name, fruit flies can be found on fruits, where they prefer to lay their eggs. Since the larvae feed not on the fruit itself but on microorganisms found on rotting material, they will especially target overripe fruits. Therefore fruit flies have evolved to identify and respond to odors emitted from ripe fruits. Red wine similarly emits odors that are attractive for fruit flies. Since fermentation is a form of controlled rotting (in both cases material is decomposed by microorganisms), the resulting compounds can be similar, which includes ethanol and 2-phenylethanol.
So, fruit flies are attracted to the smell of wine, that's why they approach the glass. Due to their small size and the surface tension, they should be able to land on water without drowning. But wine isn't water. With an ethanol concentration of 12% we see a reduction in surface tension from ~72mN/m to ~45mN/m, which is comparable to adding some detergents.
This explains, why the flies are not able to escape and drown after landing. I would nevertheless argue, that the selection pressure isn't strong enough for an evolutionary adaptation. 642
In their natural environment, they usually would not encounter large bodies of liquid which they are attracted to and which are not able to hold their weight.
In evolution and behavior we have to consider the entire species. While a few unlucky animals will die in wine glasses, most other flies will not. For them being attracted to the smell of overripe fruit and ethanol is an advantage. It's a cost and benefit calculation. For the species, the costs of a few individuals dying is outweighed by the enormous benefit of finding a good place to raise their offspring (laying eggs into rotting fruits). Therefore, the attraction to these smells and approach are positively selected for, even though this results in death in wine for some members of the species.
Watching fruit flies at a trap, you see that mostly flies land on the container rather than on the liquid, indicating that they already avoid touching down on liquid. On the other hand, fruit flies don't seem to be very acrobatic, with a part of their landings being "crash landings". Hence, some of these flies might just accidentally end up in the liquid or by walking down to the edge of the liquid and being caught up by it when touching it. Even though I couldn't find hard data on this, "landing on liquid reservoirs" doesn't seem to be necessarily a strong behavior in fruit flies, that evolution could negatively select for. 445