Fortunately, this rarely happens. But I recently had a fruit fly outbreak when I tossed something in the kitchen trash that a fruit fly was able to use to start to establish a population. One buzzing around the kitchen barely rates notice, but two or three gets one's attention.
This happened once before when I was house-sitting for someone during the summer. I had some bananas back at home, and when I returned, I found the same thing--a bunch of flies in the kitchen.
There are commercial fly traps, but I decided to try something I'd read about. It's simple: cut a plastic bottle's neck off, invert it, and put some bait in the bottom.
I used apple cider vinegar as bait with a drop of dish soap. I had experience using water with dish soap to drown Japanese beetles. The vinegar is how it became obvious that I had fruit flies to begin with: I use it in the rabbits' water, and whenever I unscrewed the bottle, I immediately had flies buzzing around me. As it turns out, it's fortunate that I used the vinegar instead of a piece of banana peel.
Some sites that describe the process for making a bottle trap like this suggest that the flies won't be able to escape, because they'll run up the sides and find their way blocked. I found that they were able to enter and exit the bottle pretty easily, which was disappointing. So I made another trap almost immediately out of a two-liter bottle to try to make it a little harder on them.
However, then I held the smaller bottle trap up and looked at it from below.
It was catching a fair number of fruit flies, but some that flew into the bottle and never touched the vinegar were able to fly back out. Over the course of a couple of days, the two traps caught more flies than I realized were present. At that point I tossed them and put out a new trap, which caught zero. I eradicated the population, or I at least got rid of all of the ones that were attracted to the vinegar.
This was easy, cheap, and effective. A triple win.