In a bar magnet, there are electrons. Some of these electrons aren't paired, meaning they don't have another electron spinning in the opposite direction. As electrons spin or move down a wire they create magnetic fields. When another electron, in the same shell, spins in the opposite direction, it creates a magnetic field with it's field upside down in relation to the other electron, this cancels out the magnetic fields of both electrons. Some atoms have unpaired electrons, but Iron, nickel, colbolt, and magnesium have many. Only magnesium has enough to make the atom magnetic.
In magnesium, there are billions of magnetized atoms that have north and south poles. The atom's poles align, and make larger segments called domains. These domains are just a group of millions of magnetized atoms. These domains align, and make the whole piece of magnesium is now magnetic. When a piece of iron, nickel, or cobolt comes near the magnesium, the magnesium aligns the atoms of it, because the iron, nickel or cobolt have south and north poles. This attracts it, and it moves to the magnesium. The atoms in the iron, nickel or cobolt are aligned with the magnesium. When you put another piece of magnesium near the magnesium, it's domains are strong enough not to conform to the other's domains.
In a piece of wire, free, unpaired, electrons flow through the wire a certain direction, creating a magnetic field. When you coil the wire, the lines of force move through the coil. A selinoid has a magnet tip, depending on which way current is flowing, the selinoid is attracted or repelled. In a relay, there is a spring loaded switch next to a coil of wire. When you apply current to the coil, the switch closes.
Sorry this is so long, I like to diagnose electronics to the subatomic level.