In the Aufbau section, we learned that electrons will fill the lowest energy orbitals first, and then move up to higher energy orbitals only after the lower energy orbitals are full. If you think carefully, though, you'll realize that there's still a problem. Certainly, 1s orbitals should be filled before 2s orbitals, because the 1s orbitals have a lower value of n, and thus a lower energy. What about the three different 2p orbitals? In what order should they be filled? To answer this question, we need to turn to Hund's Rule.
Hund's Rule states that:
When assigning electrons in orbitals, each electron will first fill all the orbitals with similar energy (also referred to as degenerate) before pairing with another electron in a half–filled orbital. Atoms at ground states tend to have as many unpaired electrons as possible. When visualizing this processes, think about how electrons are exhibiting the same behavior as the same poles on a magnet would if they came into contact; as the negatively charged electrons fill orbitals they first try to get as far as possible from each other before having to pair up.
If we look at the correct electron configuration of Nitrogen (Z = 7): 1s2 2s2 2p3
We can clearly see that p orbitals are half filled as there are three electrons and three p orbitals. This is because the three electrons in the 2p sub shell will fill all the empty orbitals first before pairing with electrons in them. If we look at the element after Nitrogen in the same period, Oxygen (Z = 8) its electron configuration is: 1s22s22p 4
Oxygen has one more electron than Nitrogen and as the orbitals are all half filled the electron must pair up.