Magnetism is one of those mysterious forces that scientists still don't fully understand. They do know that electricity flowing through certain metals, like iron, results in magnetism. The flow of electricity through a magnet creates poles and fields. These poles and fields are the reason magnets either repel or attract each other.
Discovery of Magnetism
Although ancient people had observed magnetism for thousands of years, it wasn't until 1821 that scientists began to study and understand it. While demonstrating an electric current running through a wire, Hans Christian Oersted noticed a compass needle moving nearby. After Oersted's discovery, a French scientist named Andre-Marie Ampere determined that magnetism was the result of interaction between two electric currents.
What Makes a Magnet
Magnets come in two types. Electromagnets are created by applying electricity to a conductive material like iron. Natural magnets can be anything from the Earth itself, including individually magnetized rocks like lodestone. Scientists don't understand how the electrical current that magnetizes the Earth and some of its rocks is produced.
If you hold a magnet under a piece of paper and sprinkle iron filings on the paper, the filings seem to dance around and eventually settle in a rough oval around the magnet. They are aligning themselves with the magnetic field, or the path the magnet's electricity takes to get from one end of the magnet to the other. These fields of electricity are like closed circles rotating through the magnet. Magnetic fields are the reason magnets are either repelled from or attracted to each other.
All magnets have a north and south pole. These poles are created by the fields of electricity moving through the magnets. Electricity enters the magnet at the south pole and exits it at the north pole. This is why like poles repel each other. Electricity exiting one north pole can only bump up against another north pole; it can't enter it. And it's the same story at the south pole: Electricity can only enter at the south poles, so two south poles are forced apart. However, if you line the south pole of a magnet up to the north pole of another magnet, the energy exiting the north pole of the first magnet can travel into the south pole of the second magnet, in effect creating a new magnet.