Auroras are formed when charged particles that are emitted from the sun hit the Earth's magnetic field and the atoms in the atmosphere. This interaction causes the gases in the atmosphere to give out photons of different energies, which can be seen from Earth as light.
The sun gives out a constant stream of charged particles in all directions, however, during a solar flare the concentration of particles increases. When these charged particles hit the Earth's magnetic poles, they follow the path of the Earth's magnetic field lines. Auroras occur close to the north and south poles of the planet because the magnetic field lines are most concentrated near the magnetic poles of Earth within the Arctic and Antarctic circles. These charged particles hit the nitrogen and oxygen atoms in the atmosphere at different altitudes. Depending on the atoms they hit and the altitude, the colors of the aurora change.
For example, at up to 150 miles altitude from the Earth's surface, when charged particles hit oxygen, they create red-colored light, but above 150 miles, they give off green-colored light instead. Nitrogen, on the other hand, creates a blue-colored light when below 60 miles and purple when above.