The big idea
Magnetism comes from electrons. Each electron behaves like a tiny magnet, and in most materials these cancel out. In iron, nickel, cobalt and a few alloys, neighbouring atoms line their tiny magnets up, and when enough of them point the same way, the whole object becomes a magnet.
How it works
A magnet has a magnetic field around it: the region where another magnet or a magnetic material feels a force. Field lines show the direction a compass needle's north end would point, running from N to S outside the magnet. The closer the lines, the stronger the field. Field strength is measured in teslas (T): the Earth's field at the surface is about 0.00005 T (50 microtesla), while a strong neodymium magnet gives over 1 T at its surface.
Like poles repel and unlike poles attract. A magnet also attracts unmagnetised iron, because it induces magnetism in it: the near end of the iron becomes an opposite pole. This is why attraction does not prove something is a magnet; only repulsion does.
Electromagnets. A current in a wire produces a circular magnetic field around it. Coil the wire into a solenoid and the field inside is strong and uniform, like a bar magnet's. Add a soft iron core and it gets much stronger. More turns, more current or an iron core all increase the field.
The science
Inside a magnetic material are domains: regions, typically far smaller than a millimetre, in which all the atomic magnets point the same way. In an unmagnetised piece of iron, the domains point in different directions and cancel out. Put it in a strong field, or stroke it with a magnet, and domains lined up with the field grow while others shrink or rotate. Now their effects add up.