To start, all atoms have positive protons in the center, called the nucleus, with negative electrons spinning around the nucleus. Nonmagnetic atoms have electrons that spin in all directions.
The electrons in the atoms of magnetic material spin around the nucleus in mainly only one direction. This creates a more negative side and a more positive side in the magnet which makes the north pole and the south pole of the magnet. This difference creates a high attraction on one side of the magnet where the electrons are spinning to the other side of the magnet where the electrons are not spinning. This magnetic force flows from the north pole to the south pole outside of the magnet. This flow creates a magnetic field around the magnet. The force is stronger closer to the magnet.
Connecting north pole to south pole of different magnets makes the electrons all spin in the same direction. That is a strong attraction.
Trying to connect south pole to south pole or north pole to north pole is like trying to force the electrons to reverse the direction that they are spinning. That is why the like poles repel each other.
Although you didn't ask, you may want to know why only some materials are attracted to magnets. Certain materials, mainly iron, nickel, and cobalt, have atoms that are easily rearranged by a magnet. The electrons in iron rearrange in the appropriate way to spin according to the magnetic field which creates an attraction to either pole. MRI machines realign the electrons inside the body in a similar manner to produce an image for the diagnosis of medical conditions.
By applying an electrical current through a coil of wire, the electrons can be forced to spin around mainly in only one direction. This is how an electromagnet is made. Wrapping the coil around an iron core increases the strength of the magnet because the current makes the atoms in the iron spin in the same direction too. Thus, the iron becomes a temporary magnet. Very strong electromagnets are made that will lift large amounts of iron. The advantage of this is that once the current is stopped, the magnetic field ceases to exist, and the material is released.