Moths fly by night, but can have very good night vision. Of course, they need some light to see by, particularly moonlight and starlight.
Moths and other night-flying insects fly in a straight line by reference to the relative direction of the moon. Because the moon is so far away, the direction to the moont is fixed at any one time, no matter how far the moth flies (and the moths have evolved to take the moon's direction of travel across the sky into account).
However, when they see a bright light close by, and confuse it with the moon,their navigation instinct gets out of whack. In this case, instead of being constant, the angle to the light varies as the insect flies past. So instead of travelling in a straight line, they fly in a curve around the object, when trying to keep the source of light at a constant angle to their flight path. (That is why the spiral inwards towards the source of light).
Incidentally, insects (and birds) that fly by day navigate by the sun; but there is less chance of another bright source of light confusing them.