Not all bugs are attracted to artificial light, for example, the butterflies and even cockroaches won't do this. The ones that are, however, seem to be so because of confusion. Bugs being attracted to artificial lights are actually following a normal instinct called phototaxis.
In ages past, there were only natural lights (stars, moon, and sun). Nocturnal bugs were guided by the stars and moon while bugs that thrive during the day rely on sunlight to see. When a light goes on outside, these bugs get confused and usually swarm to the light for guidance to find food. For some bugs, the artificial light is too bright and throws their natural instincts off. These bugs just dive straight for the artificial light (these would be the bugs you see, and hear, constantly slam themselves into the bulb or around it). Some bugs that are usually moving about during the day may think that the light means it is day light.