The big idea
Every magnetic field comes from moving charge or from the intrinsic magnetic moment (spin) of particles such as electrons. A permanent magnet is a material in which an enormous number of electron spins are locked into alignment, so their tiny fields add to a large one.
How it works
In most atoms, electrons pair up with opposite spins and their moments cancel. Atoms of iron, cobalt, nickel and many rare-earth elements have unpaired electrons, so each atom has a net magnetic moment. In a ferromagnet, a quantum-mechanical effect called the exchange interaction makes it energetically favourable for neighbouring moments to point the same way. Alignment spreads over domains, separated by thin domain walls where the direction rotates.
A sample splits into domains because a single domain would have a large external field storing a lot of energy. Applying a field moves domain walls (domains aligned with the field grow) and, at high fields, rotates the moments. If the material resists returning to the jumbled state, it stays magnetised.
Thermal agitation fights alignment. Above the Curie temperature the order collapses: about 770 °C for iron, but only about 310 °C for the neodymium-iron-boron compound, which is why heat is the enemy of strong magnets.
The physics
Fields from currents. A long straight wire carrying current produces circular field lines with flux density