When a small magnet with magnetic moment \vec{\mu} is placed in a magnetic field \vec{B}, its energy is -\vec{\mu} \cdot \vec{B}. If the field is the same everywhere, the magnet only receives a moment of force that tries to turn it, and as a whole it does not move. This is because the forces on the N pole and the S pole have the same size and opposite directions.
When the strength of the field changes from place to place, the forces on the N pole and the S pole differ in size, so the magnet as a whole receives a force. In a field whose strength changes along z, the force is F_z = \mu_z\dfrac{\partial B_z}{\partial z}, proportional to the z component \mu_z of the magnetic moment. If \mu_z is positive, the magnet is pushed toward the stronger field; if negative, toward the weaker field.
Stern and Gerlach tried to measure the z component of the magnetic moment of atoms with this force. Silver atoms evaporated by heating in an oven were passed through narrow slits to form a thin atomic beam, which was sent through a strongly varying field between a pointed pole and a grooved pole. The field is stronger closer to the pointed pole.
The figure shows the apparatus from the side. Atoms that have passed through the field stick to a glass plate, shifted up or down according to the z component of their magnetic moments. The experiment was carried out in Frankfurt in 1922.
A silver atom is electrically neutral, so it receives no force from the magnetic field as a charge. It is deflected because the atom has a magnetic moment.
If the magnetic moment of an atom could point in any direction, like a classical magnet, \mu_z would take every value from -\mu to \mu. The atoms leaving the oven point in random directions, and if every direction appears with the same probability, \mu_z is spread evenly over this range. The mark on the glass plate should then be a single thick mark stretching continuously up and down.
The actual mark was two lines, one above and one below. Nothing landed in the middle. \mu_z takes only two values, and this was the first experiment to show directly that the direction of a magnetic moment is discrete.
The split was about 0.2 mm at most. The left of the figure is the classical prediction and the right is the shape of the actual mark; the dots show atoms landing one at a time.
The two lines join at the left and right ends of the mark because the cross section of the atomic beam is long and narrow in the horizontal direction, and toward its ends the field strength changes less steeply. The shift is proportional to \dfrac{\partial B_z}{\partial z}, so it is large in the middle and small at the ends.
As seen in the article on angular momentum, the z component of orbital angular momentum takes 2l + 1 values. Since l is an integer, this number is always odd. If the magnetic moment came from orbital angular momentum, the line should split into an odd number of lines, 1, 3, 5 and so on, never 2.
Moreover, in a silver atom, 46 of the 47 electrons cancel one another's angular momentum, and the remaining one is in the 5s state. 5s has l = 0, so its orbital angular momentum is 0, and by the orbital picture a silver atom should not be deflected in the magnetic field at all.
The split into two can be explained by supposing that the electron itself has an angular momentum whose z component takes two values. If s is the number giving the size of this angular momentum, the number of z components is 2s + 1 = 2, so s = \dfrac{1}{2}. The z component takes the two values S_z = \pm\dfrac{\hbar}{2}. This angular momentum is called spin.
The size of the spin is \sqrt{s(s+1)}\,\hbar = \dfrac{\sqrt{3}}{2}\hbar = 0.866\hbar, larger than the z component \dfrac{\hbar}{2}, so as with orbital angular momentum, it never lines up with the z axis. The figure places the three directions for l = 1 and the two directions for s = \dfrac{1}{2} side by side at the same scale.
In 1925 Uhlenbeck and Goudsmit proposed, in order to explain how spectral lines split, that the electron has an angular momentum corresponding to rotation about itself. The result of Stern and Gerlach came to be explained by spin after that. In 1927 Phipps and Taylor confirmed that hydrogen atoms in their lowest state also split into two lines.
Spin does not mean that the electron is a small ball actually spinning. It is a property the electron has from the start, with no counterpart in classical mechanics.
The z component of the magnetic moment that comes with the spin of the electron is \mu_z = -g\dfrac{e}{2m_\mathrm{e}}S_z, where g is about 2.0023. For orbital angular momentum, the corresponding value was 1. Spin carries about twice as much magnetic moment for the same amount of angular momentum. Putting in S_z = \pm\dfrac{\hbar}{2} gives \mu_z \approx \mp\mu_\mathrm{B}.
In a nonuniform magnetic field, a silver atom receives the force F_z = \mu_z\dfrac{\partial B_z}{\partial z}. For example, if \dfrac{\partial B_z}{\partial z} = 1000 T/m, the size of the force is 9.3 \times 10^{-21} N. For a silver atom of mass 1.79 \times 10^{-25} kg, the acceleration is 5.2 \times 10^4 m/s².
If the magnet is 3.5 cm long and the atoms move at 550 m/s, an atom spends 6.4 \times 10^{-5} s inside the magnet. In that time it shifts up or down by \dfrac{1}{2}at^2 = 0.105 mm, and the upper and lower lines end up about 0.21 mm apart. This is about the same as the width seen in the previous scene.
The figure draws the paths of atoms through the magnet in this example, with the vertical shift enlarged. Inside the magnet the force is constant, so the paths are parabolas. Here the glass plate is placed at the exit of the magnet.
That the g of electron spin differs slightly from 2 was measured by Kusch and Foley in 1947. It agreed with the value Schwinger calculated with quantum electrodynamics the following year, and became one of the results that confirmed that theory.
If only the atoms deflected upward are picked out and sent through another device along z, all of them are deflected upward. A state with S_z fixed at +\dfrac{\hbar}{2} gives the same value when measured again.
If the atoms deflected upward are instead sent through a device whose field changes along x, half go to S_x = +\dfrac{\hbar}{2} and half to S_x = -\dfrac{\hbar}{2}. In a state whose z component is fixed, the x component is not fixed.
If only the atoms that went to + in the x device are picked out and sent through a z device again, they once more split half up and half down. That they were S_z = +\dfrac{\hbar}{2} at first is no longer kept after the measurement along x.
The numbers in the figure are fractions of the atoms sent in at the start. Atoms that split downward are stopped and not sent into the next device. The state S_x = +\dfrac{\hbar}{2} can be written as the two z states superposed with coefficients of \dfrac{1}{\sqrt{2}} each. The probability of going up or down is the square of that coefficient, \dfrac{1}{2}.
As seen in the article on angular momentum, the components of angular momentum are not fixed at the same time. The same holds for spin, with S_xS_y - S_yS_x = i\hbar S_z. The result of the devices in a row shows this relation as it is.