Moths are attracted to light because they use the moon to orientate themselves in flight. In the presence of artificial lights, moths often become confused and will mistake the light source as the moon.
For a moth, the moon appears at optical infinity, that is, the rays of light appear parallel to the Earth when viewed through the eyes of a moth. This makes following the moon a particularly useful navigational tool, especially for linear flight. Optical infinity allows the moth to constantly orientate its location in relation to space. This is very important, considering the darkness of night, where other visual landmarks may not be visible.
Artificial lighting can appear brighter than the moon, and moths will mistake the stronger light source as the moon. While the moon is in a fixed position in the sky and will stay at a stable distance as the moths fly, moths can fly closer toward artificial lights. As they fly closer, they lose optical infinity and become disorientated.
A moth may constantly circle artificial lights in an attempt to re-orientate itself. The circling is an attempt at keeping the light source as a constant frame of reference so that it can triangulate its location in space. However, this task is made impossible since the light source is not at optical infinity. As a result, a moth may appear to be stuck at a light to the point of exhaustion.