According to the explanation of the phrase "like a moth to the flame" at The Phrase Finder, "the word moth was used the the 17th century to mean someone who was apt to be tempted by something that would lead to their downfall." But why do moths have this fatal attraction anyway? National Geographic explains in the above video:
The theory is that these primarily nocturnal insects have evolved to travel by the light of the moon and stars. This way of travel is called transverse orientation. An easy way to think about transverse orientation is to imagine a sailor travelling in the direction of the North Star. In theory, moths similarly follow the light source at a precise position and a precise angle to their bodies. This is how moths would navigate for millions of years … by the light of the moon. What moth evolution couldn’t account for was the proliferation of constant electric light in our modern world. When Thomas Edison patented the lightbulb on January 27, 1880 it was a bad day in moth history. These lightbulbs began to act as artificial moons, confusing moths and overwhelming their senses. Since moths are accustomed to orienting to distant light sources, they can be easily disoriented when a closer light source, like a porch lamp, comes into view.