Similar poles repel each other because of magnetic lines of force. A magnetic line of force originates from the north pole of a magnet and ends at its south pole. When the north and south pole ends of a magnetic object are put into close proximity, they form an attraction because the magnetic lines continue together in the same direction. Like poles repel because the lines of force are head-to-head.
At the atomic level of all material, electrons occupy various electron shells and sub-shells around the nucleus of an atom. Each sub-shell can hold two electrons, one with an up spin and one with a down spin. This difference in spin creates a magnetic dipole, the source of magnetism. Most materials have atoms that are randomly oriented, so the difference in poles cancels each other out. However, ferrous materials such as iron and nickel contain magnetic domains, which are clusters of atoms with dipoles oriented in the same direction.
Magnets contain atoms with dipoles permanently oriented in the same direction, which creates a magnetic field with both a north and south pole end. When a magnet comes into contact with a ferrous material, the magnet incites the magnetic domains of the ferrous material to orient their dipoles in the same north and south direction. This north and south dipole direction of all atoms creates the magnetic line of force.