To understand it, you have to start with electrons.
Everything that ever existed—from a giraffe, to a magnet, to the Insane Clown Posse—is made up of atoms. This is the basic unit of matter, and it's so tiny that you can't see it without some very specialized equipment. But that tiny atom is made up of even smaller things. At its heart is a ball of particles called the nucleus, which contains protons and neutrons—the protons have a positive electric charge, while the neutrons, appropriately, have no charge.
Meanwhile, outside the nucleus, are the negatively-charged electrons.
It's useful to imagine the atom as something like the carousel at the county fair. Just like the carousel has a central pipe organ that has ponies constantly spinning around it, the atom has a central nucleus with electrons going round and round it. This analogy isn't accurate. But it's what physicists call a "useful fiction"—an easy way to wrap your head around a concept that is totally different from anything we experience in our everyday lives. There are a lot of useful fictions involved in talking about the particles that make up an atom. Case in point: Spinning electrons.
As they circle around the nucleus, each electron also spins on its own axis—so, maybe the atom is less like a carousel, and more like a spinning teacup ride. Again, this is a useful fiction. Bonasera and Kakalios are careful to point out that electrons do not actually spin like a top. But just like each electron has a mass and a negative electric charge, they each have another property, as well. One that's very, very difficult to describe.
Electrons possess an intrinsic angular momentum, independent of any orbit around a nucleus. Physicists call that momentum "spin", in that the electron's internal rotation can be clockwise, or counter-clockwise. But, when they do that, they're really just using familiar words, and familiar mental images, to talk about something completely unfamiliar.
All of this is important because electron spin is central to explaining how those fucking magnets work.
Because electrons spin—because they have this hard-to-describe property that we call "spin"—each electron produces a small magnetic field. In fact, Kakalios told me that, originally, some physicists had thought that electrons might, literally, spin. They thought that because they knew that if you took something with an electric charge—like the electron has—and spun it fast enough, it would create a magnetic field. That literal interpretation doesn't work. To produce their observed magnetic field by actually spinning, each electron would have to turn on its axis faster than the speed of light. But it helps you understand the concept: Electrons produce a magnetic field because they spin.
Every atom has electrons, so every atom has a magnetic field. But why, then, are only some materials magnetic? Why doesn't your couch repel other couches? Why can't we stick two cats together?
In cats, couches—all the everyday things that aren't magnetic—the magnetic fields produced by electrons simply cancel each other out. For every electron that's spinning clockwise, there's another electron spinning counterclockwise. All electrons produce magnetic fields, even the ones in cats and couches. But cats and couches aren't magnetic because their electrons' magnetic fields interfere with one another and keep the overall magnetic force so weak as to be nonexistent.
Magnets are different.
In a piece of iron, most of the electrons still cancel each other out—there's a clockwise spinner for every counterclockwise spinner. But, there are also a few stragglers, electrons which have no opposite-spin partner. Their magnetic fields don't get counteracted. When we see two pieces of iron stick together, or repel one another, we're seeing the work of those straggler electrons.
Remember back in third grade, when you learned about what magnets do, and you were able to magnetize a nail by rubbing a bar magnet against it? That's the stragglers again. See, all the un-partnered electrons in a single piece of iron aren't necessarily working together. In that nail, the stragglers are like squabbling city-states, Joel Bonasera says. Those city-states are called "domains". But domains easily fall into line behind a strong leader. So, when you rub the bar magnet over the nail, the straggler electrons in the nail all snap to attention. Their magnetic fields line up with the magnetic fields in the electrons of the magnet. Now, the nail doesn't just have a lot of tiny, disconnected magnetic field domains. It has one, unified magnetic field. It becomes a magnet that we can see on the macro level.
And that's how magnets work, to the first value of why. It's fascinating, it's mind-boggling, and, as Jim Kakalios put it, "Insofar as the entire Universe is a 'miracle', this is, too."