A state of the electron in a hydrogen atom was fixed by three quantum numbers, n, l and m. Because the electron has spin, one more is added: m_s = \pm\dfrac{1}{2}, the z component of the spin. For each (n, l, m) there are two directions of spin.
Including spin, the number of states with principal quantum number n is 2n^2: 2, 8, 18 and 32 for n = 1, 2, 3, 4. Each square in the figure stands for one set of (n, l, m), and the up and down arrows in it are the two states of spin.
The 18 states for n = 3 break down into 2 for 3s, 6 for 3p and 10 for 3d. Each time l increases by 1, the number of values of m increases by 2, so the number of electrons that one l can hold is 2(2l + 1): 2, 6, 10 and 14 for s, p, d and f.
The numbers 2, 8, 18 and 32 also appear in the lengths of the periods of the periodic table. The actual lengths of the periods, however, run 2, 8, 8, 18, 18, 32, 32. The reason is explained in the article on the periodic table.
Counting states matters for the structure of atoms because the number of electrons that can occupy one state is limited to one.
In 1925 Pauli proposed that no two electrons in an atom can have all four quantum numbers the same. This is called the Pauli exclusion principle. The same (n, l, m) can hold at most two electrons, with opposite spins.
Pauli proposed this principle from the study of spectra. In 1924 Stoner had recounted the number of electrons that go into each shell, using the way spectral lines split. Taking this as a starting point, Pauli supposed that the electron has a property that takes two values and cannot be described classically, and added a fourth quantum number. Within the same year, this property was explained as spin.
The two electrons of a helium atom are both in 1s, with opposite spins. This is the state on the left of the figure. The state on the right, with two electrons of the same spin in 1s, has all four quantum numbers the same and is not allowed.
A third electron cannot go into 1s, so in a lithium atom the third electron goes into 2s. An electron in 2s spreads farther out than 1s, and part of the charge of the nucleus is canceled by the two electrons in 1s. As a result, the 2s electron is only weakly attracted to the nucleus, and lithium is an element that easily loses one electron to become a positive ion.
The exclusion principle holds not only for electrons but also for protons and neutrons. The way protons and neutrons fill states of definite energy in a nucleus is explained by the same principle.
In classical mechanics, even two particles of the same kind can be told apart by following the path each one takes. In quantum mechanics the paths of particles are not fixed, so there is no way to distinguish a state with two electrons swapped from the original state.
If the wave function of two particles is \psi(x_1, x_2), the probability |\psi|^2 does not change when they are swapped, so \psi(x_2, x_1) = \pm\psi(x_1, x_2). The wave function of electrons always has the minus sign, that is, its sign flips when the electrons are swapped. Such particles are called fermions, and protons and neutrons are fermions too. Particles with the plus sign are called bosons, and the photon is an example.
When the first electron is in state a and the second in state b, the wave function whose sign flips on swapping is \psi = \dfrac{1}{\sqrt{2}}\left(\varphi_a(x_1)\varphi_b(x_2) - \varphi_b(x_1)\varphi_a(x_2)\right). If a and b are the same state, this expression is 0. No wave function can be made with two electrons in the same state, and this is the exclusion principle.
For two particles placed one each in the lowest and the second state of a box of length L, the figure draws the probability |\psi(x_1, x_2)|^2 with the position of the first particle on the horizontal axis and the second on the vertical axis. For fermions, the probability is 0 on the diagonal x_1 = x_2, and two electrons with the same spin never come to the same place. For bosons, on the contrary, the probability is large near the diagonal.
In 1926 Fermi and Dirac each derived the statistics of a large collection of particles with this property. That particles with half-integer spin, such as \dfrac{1}{2}, are fermions, and particles with integer spin, such as 0 or 1, are bosons, was shown by Pauli in 1940 using relativity.
In the lowest state of an atom, electrons occupy states in order from the lowest energy, one electron per state. The figure shows how the electrons go in as they are added one at a time, from hydrogen with atomic number 1 to argon with 18.
In atoms with two or more electrons, the other electrons cancel part of the charge of the nucleus, so the energy also depends on l. This is called shielding. 2s also has probability right near the nucleus, so it is shielded less and has a lower energy than 2p with the same n = 2.
When electrons go into the three 2p orbitals of equal energy, they first go into separate orbitals with their spins pointing the same way. This is called Hund's rule. In the figure too, the two 2p electrons of carbon and the three of nitrogen each go into a separate orbital pointing up, and electrons pointing down are added from the fourth, in oxygen.
When 1s is filled by 2 electrons the atom is helium, when the 8 of 2s and 2p are filled it is neon, and when the 8 of 3s and 3p are filled it is argon. All of them are noble gases that hardly react. The breaks between the groups of states that electrons fill match the breaks at which the properties of the elements repeat.
How the electrons are placed is written with the names of the orbitals and the numbers of electrons in them, as in 1\mathrm{s}^2\,2\mathrm{s}^2\,2\mathrm{p}^4. This is called the electron configuration, and the expression at the bottom of the figure is the electron configuration of that atom.
Without the exclusion principle, every electron of every atom would go into the lowest state, 1s. The larger the atomic number, the larger the charge of the nucleus, so the electrons would be pulled in more strongly and atoms would keep getting smaller. Actual atoms do not get smaller as the atomic number increases; all of them are around 0.1 nm in size.
The figure shows how particles fill a box of length L as they are added. The levels in the box are E_k = k^2E_1. Fermions with spin \dfrac{1}{2} can go only two to a level, so as the number of particles N grows, the top occupied level rises to about the \dfrac{N}{2}th. Bosons can all go into the lowest level.
For large N, the total energy of the fermions is about \dfrac{N^3}{12}E_1, growing in proportion to the cube of N. E_1 is proportional to \dfrac{1}{L^2}, so the narrower the box the same number of particles is squeezed into, the more the energy rises, and the particles exert a force pushing the box outward. This pressure is called degeneracy pressure.
This effect is why matter keeps a fixed volume instead of collapsing. In 1967 Dyson and Lenard showed that electrons obeying the exclusion principle is essential for matter to exist stably.
A white dwarf is a star with about the mass of the Sun packed into about the size of the Earth, and the degeneracy pressure of its electrons holds it up against collapse by gravity. Around 1930 Chandrasekhar showed that there is an upper limit to the mass that electron degeneracy pressure can hold up. The limit is about 1.4 times the mass of the Sun.