To understand this phenomenon, you need to know about phototaxis. It is an organism's automatic movement toward or away from light. Moths are positively phototactic. They seem charmed by your porch light, your headlights or your campfire. 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 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.
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 tends to be a more advantageous response than flying toward darkness.
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. 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.
（来源：coolquiz.com 英语点津 Annabel 编辑）