On any given night, hundreds of moths, flies and other insects can be seen making endless circles around street lamps and porch lights. This may seem like a exercise in futility or further proof that insects are not the sharpest tools in nature's shed, but there are actually several theories on why insects attracted to light make such nightly pilgrimages. There is no single scientific explanation for this behavior, however.
Not all insects attracted to light act on those impulses. For some insects, a bright light source is seen as a emergency beacon. When in doubt, they will instinctively head for the light, which is generally higher than their current danger-filled position. Moving towards the dark would be seen as a move downward, which can be even worse than not moving at all. Light for some insects could be viewed in the same way as air bubbles pointing the way up to the surface for other creatures.
Another popular theory is that insects attracted to light use it as a navigational aid. An insect flying north, for example, could judge its direction by keeping a natural source of light such as the sun or moon on its right side. This method works well as long as the source of light remains constant and at a distance. If an insect encounters a round incandescent porch light, however, it becomes confused by the light source. A moth will continue to circle a light because it instinctively wants to keep the light on a certain side of its body while navigating.
The difference between insects attracted to light and insects not attracted to light is a phenomenon known as phototaxis. Certain insects such as cockroaches or earthworms have negative phototaxis, meaning they are repelled by exposure to light. Moths, flies and many other flying insects have positive phototaxis, meaning they are naturally attracted to light.
There is some debate in the scientific community over why a positively phototactic insect will continue to hover around an artificial light source even if a natural light source becomes available. Some believe the insect is not actually attracted to the light itself, but the darker areas which surround it. Others suggest the insect's eyes, which often contain multiple lenses, cannot easily adjust from light to dark, leaving the insect vulnerable to predators while night-blind. It may be safer for the insect to remain in the light rather than fly away and become too blind to react to threats and obstacles.
Hope I helped.