First of all, that's a really interesting question and it is my pleasure to actually discover the answer.
(C&P'd from the link below)
To understand this phenomenon, you need to know about phototaxis. Phototaxis is an organism's automatic movement toward or away from light. Cockroaches are an example of a negatively phototactic organism. You've probably noticed how they scurry back into dark corners and crevices when you illuminate their late-night snacking party in your kitchen. Moths are positively phototactic. They seem charmed by your porch light, your headlights or your campfire (even if it leads to their untimely demise). While there is no definitive explanation for this phenomenon, there are some interesting theories.
Some types of moths are known to migrate, and it's possible that the night sky gives them navigational clues. A moth's up-down orientation might depend in part on the brightness of the sky relative to the ground. Some lepidopterists (moth and butterfly scientists) suggest that moths use the moon as a primary reference point and have the ability to calibrate their flight paths as the Earth's rotation causes the moon to move across the sky. (There is even evidence to support the theory that migrating moths have an internal geomagnetic compass system to guide them in the right direction.) So a moth's attraction to an artificial light or to a fire could be related to orientation, and lead to disorientation -- the moth wasn't "expecting" to actually get to "the moon" (the light source) or to be able to fly above it, so confusion results.
It's also possible that moths have an escape-route mechanism related to light. Imagine disturbing a bush-full of moths at night -- they all fly up and out of the bush, toward the sky. To a moth in danger, flying toward the light (which is usually in the sky, or at least upward) tends to be a more advantageous response than flying toward darkness (which is usually downward).
Moths are more sensitive to some wavelengths of light -- ultraviolet, for example -- than they are to others. A white light will attract more moths than a yellow light. Yellow is a wavelength moths don't respond to.
Another interesting question is: Why do moths stay at lights? A moth's eyes, like a human's eyes, contain light sensors and adjust according to the amount of light the sensors detect. In high illumination, light from each of the moth's thousands of fixed-focus lens facets is channeled to its own sensor (ommatidium). In low illumination, light from multiple lenses is channeled to the same ommatidium to increase light sensitivity. You probably experience a few moments of blindness when you turn on a bright light after your eyes have adjusted to darkness, or when you are suddenly in darkness after being in bright light. A moth's dark-adapting mechanism responds much more slowly than its light-adapting mechanism. Once the moth comes close to a bright light, it might have a hard time leaving the light since going back into the dark renders it blind for so long. In the case that the moth escapes, it won't remember the problem with flying too near the light and will probably find itself in the same predicament all over again.
Another possible explanation for why moths stay at lights is that they are mostly night-flying creatures and eventually respond to the light as they would to the sun -- by settling in for their daytime "sleep."