This is a quantum mechanical phenomenon that is difficult to make a comparison to, but can be thought of as similar to the rotation of the Earth about its axis. Electron spins are in one of two states, up or down. Electrons are arranged in shells and orbitals in an atom. Secondly, domains which are aligned with the field will expand to take over regions occupied by domains aligned opposite to the field. This is what is meant by magnetizing a piece of iron.
Iron comes in two forms, hard and soft.
In hard iron, the domains will not shift back to their starting points when the field is taken away. In soft iron, the domains return to being randomly aligned when the field is removed. Hard iron is used in permanent magnets. To make a permanent magnet, a piece of hard iron is placed in a magnetic field. The domains align with the field, and retain a good deal of that alignment when the field is removed, resulting in a magnet.