Houseflies are attracted to decaying organic matter which they use both for food and as a place to lay eggs. They're also attracted to sugar and non-decaying food matter, but they're primarily interested in moist, rotting food and decomposing garbage.
Despite the name "housefly," this common insect is found indoors and outdoors all over the world. It's one of the most widely distributed insects and accounts for over 90 percent of all flies in human habitations. Houseflies are common on farms, and any place animals range, due to the large quantities of fecal matter found in these locations. Manure is not only a good food source for houseflies but also a prime medium for egg laying because it provides warmth and nourishment for the maggots after they hatch out.
Flies are most active during the heat of day because higher temperatures speed the rate of decomposition, making the scents of their food sources more attractive. The entire life cycle of a housefly only lasts about three weeks under optimal conditions. Eggs normally hatch about a day after being laid. Within a few days, the maggots transform into pupae. Two to three days later, the adult flies emerge. An adult fly usually only lives about two weeks in the wild. During this time, the female housefly can lay as many as 9,000 eggs.