Explaining what happens is comparatively easy but how it happens is much more difficult although you can always say that a system tries to move to its lowest energy state and is the simple answer to your question.
The origin of magnetism is the fact that electrons have spin angular momentum and as a result they behave as though they are microscopic bar magnets.
If the total spin of an atom is zero ie all the electrons are paired, then a phenomena called paramagnetism is exhibited which means that the material actually repels an external magnetic field.
How to levitate pencil lead is a nice do it home type video showing this for graphite.
Although all atoms could exhibit diamagnetism that effect is very often swamped by an effect due to unpaired electrons in an atom called paramagnetism.
In this instants the atomic scale magnets due to unpaired electrons line up when in an external magnetic field because that is a lower energy state than if then not lining up.
However removal of the external state means that due to thermal agitation the material reverts back to an demagnetised state.
For the magnetic materials you are asking about even without a magnetic field neighbouring atoms in a region become aligned within a region called a domain.
If the magnetism of the domains within the substance are randomly align the substance overall is not magnetised.
Applying an external field tends to align the domains to produce a magnetic field in the same direction as the external field and this is a a lower energy state.
The material is magnetised.
The alignment of the atomic bar magnets is always battling with the thermal vibrations of the atoms and so for some ferromagnetic materials, eg iron, removal of the external magnetic field rests in the thermal processes randomising the magnetism due to the domains and the material ceases to be magnetised.
However for something like steel the thermal agitation is not enough to change the orientation of the magnetism due to the domains and with the external field removed the material stays magnetised.
There is a nice diagram in @annav's answer to the question Direction of electric dipole moment and magnetic dipole moment which illustrates my point about lowering the energy state.
Imagine an atomic scale bar magnet in an external field as shown in the left hand diagram.
That bar magnet has a torque applied on it which will try and rotate the bar magnet so that it is orientated more in the direction of the external field with the torque being zero when the bar magnet is parallel to the external field.
In moving closer to being parallel to the external field the torque can do work which means in undergoing that reorientation the system (bar magnet and external field) has less potential energy ie moved to a more stable state.