Why insects are attracted towards light?
Phototaxis is a common behavior to many species, including insects but not only (e.g. most fishes do phototaxis).

The ethological reason is quite simple: with light you can see, and if you see you can find food, avoid predators and increase your survival chances. Phototaxis is thus a very strong innate reflex: at the neural scale in many species it is "hardwired". 

I would say that the initial "purpose" of this reflex was to avoid shade areas during daytime, the light during nightime being pretty rare before man invented artificial light. The fact that insects gather around artificial lights during nightime is, to me, a side effect of this reflex.