Fruit flies can come into your house already on the fruit or fly in through windows and doors. They feed and reproduce on soft, decaying fruit and vegetables, which is why you might also see them swarming around your fruit bowl once stuff starts to ripen. We see them most often near our garbage, and we have one friend who composts and freezes her scraps before adding them to the bin, in order to kill any fruit fly eggs.
Re-Nest posted a genius solution for trapping and killing fruit flies, which involves a cone of paper over a shallow glass of vinegar. There's even a pdf template for the cone!
Click here: Fruit Fly Death Trap from AT: Re-Nest
Related: How To Use Up Overripe Fruit