Electromagnets are complicated but let me offer you the beginning of an explanation.
An electromagnet is made of a piece of iron with wire wrapped around it.
When an electric current flows through the wire it makes a weak magnetic field typically around 0.001 to 0.010 tesla. For reference the Earth’s field is typically less than 0.0001 tesla at the Earth’s surface.
When the iron is exposed to this weak field it becomes magnetised. All materials become a little bit magnetic when exposed to a magnetic field but iron is exceptional. If the magnet is constructed well, fields more intense than 1 tesla can be created.
What is special about iron?
As in atoms of all substances, electrons in atoms of iron arrange their orbits so as to minimise their mutual electric repulsion. This causes them to orbit the iron nucleus in one direction. This is quite common. But in iron, electrons in neighbouring atoms also orbit in the same sense. Again this is because of the electrical repulsion between electrons. The result is that microscopically each atom acts like a little loop of electric current – a tiny magnet – and that all the neighbouring atoms of are lined up with the loops in the same direction.
Although all the atoms are aligned with their neighbours, normally one region of iron – called a domain – balances out the magnetic field of other regions. This is like the way small bar magnets spontaneously align themselves so as to cancel out each others magnetic effect.
If the iron is prepared correctly, even the weak field of the wire is able to make some domains grow and others shrink – amplifying the effect field of wire.
I told you it was complicated!