Bugs are attracted to light because they use the moon to navigate, and artificial light mimics the light of the moon. Bright outdoor lighting is a relatively recent invention, and insect evolution has not adapted to it yet.
Until the creation of light bulbs, the moon was usually the brightest light in the night sky. Artificial light confuses insects because it radiates in all directions, preventing bugs from flying toward their fixed locations. As a result, insects keep rotating around manufactured sources of light, interfering with their normal hunting and mating activities, and leading to the decline of some species.