There's a title and a half for ya! These flies are problems in food handling establishments mostly, places like restaurants cafeterias and bars, but sometimes they find there way into the homes of people as well.
The most common fly is the drain fly. These flies come from filth. In food handling establishments they will emerge from the floor drains or garbage disposals where they breed in the gelatinous film that covers the walls of the pipes. They a feisty buggers with their eggs hatching in 2 days and they are sexually mature at birth. So you can see how they are such a successful insect. A similar fly compared to this one is the phorid fly who also breeds in the drains and garbage disposals and dumpster and trash cans. Any wet, damp area. Problems in restaurants are many. There are so many areas that have moisture, the mop closet, tiles pulled away from the wall get wet and stay wet, dishwashing area, floor drains, dumpster have decaying matter in them. Controlling these flies is difficult. Using a combination of fly light traps and manual cleaning and drying of the above areas works best. The floors drains often times need to be scrubbed out with a floor drain brush to break up all the gelatinous film that is on the drain walls, then regular treatment with a good bio drain gel will help to maintain your hard work. Fixing any cracked or broken tiles on the floor and walls is also good. Keeping the trash cans clean and DRY will help. It's an integrated approach for sure, but it will work.
In the home is you start seeing these buggers then the story is a bit more grim. It most likely means a broken soil pipe under the slab or in the basement. SO this will need to be checked with a camera that most plumbers have access to. They can also be coming up from your drains, pr their might be a leak under your bath tub or the seal of the toilet might be broken. These are all possibilities. An experienced exterminator will be able to help you in determining the source and the remedy is to fix the problem. Once dry then the flies will go away. Important to note is that if you have a broken soil pipe under a slab you will have to ask the contractor to remove all the wet soil and replace it with dry new soil, even with the pipe fixed the flies will breed for a long time in that moist dirt.