According to pest-control company Orkin, different types of flies are attracted to different substances and materials. Some types of flies are drawn to fecal matter and rotting meat, including carcasses. Others are attracted to sugary and vinegary substances. Still others congregate near sewage and other filth.

...
Many types of flies are attracted to waste and filth.

House Flies

House flies, also known as "filth flies," have the ability to harm humans and animals. They're attracted to garbage, manure and carrion. Once they land on these substances, they transport bits of them to people when they land on food and surfaces where food is prepared. House flies not only regurgitate their stomach contents onto food before ingesting it again, they also defecate on food. House flies are not picky about the foods or filth that they consume.

Fruit Flies

Fruit flies, sometimes called "vinegar flies," like sweet and fermented liquids, including soda pop, vinegar, rotting fruit, syrup and alcoholic beverages. These types of flies get nutrients from fermented liquids and lay their eggs in and around such substances. Fruit flies typically are found in kitchens and other areas where there are lots of sugary items.

Drain Flies

Drain flies are similar to "filth flies" in that they're drawn to sewage and garbage. They're drawn to dirty substances that accumulate in bathrooms, especially raw sewage. They lay their eggs inside gunk that accumulates in toilet and sink drains and their larvae survive inside of this filth.

Fungus Gnats

Fungus gnats, which look like mosquitoes, live in moist areas where fungus -- what they eat -- grows. Pigeon droppings and over-watered plants that develop fungus attract these gnats, and infestation can occur. Fungus gnats pose health problems when they land on food and surfaces where food is prepared.