How do electromagnets work?

By passing D.C. current through a conductor wound around a core, a magnetic field is developed around the current carrying conductor and work as a magnet.