An electromagnet is a device used to generate a magnetic field with the help of an electric current. The principle that electromagnets work by is used in a wide range of electric and electronic devices such as electric motors, solenoids, tape drives and even speakers and microphones. The simplest electromagnet you can build consists of a simple metal wire through which electric current is being circulated.
But before we get into more intricate details, let's talk about magnets. Magnets occur naturally in the Earth's crust in the form of magnetite, a type of permanent magnets, meaning that they produce a constant non-variable magnetic field around them. A typical magnetic field presents two poles, north and south (the situation is analogue to electric charge, which can be either positive or negative). Opposite poles manifest attraction forces towards each other, while poles alike repel each other.
Electromagnets on the other hand are devices that produce only temporary, variable and non-variable magnetic fields. When electric current starts flowing through a conductor wire, it will generate a small circular magnetic field around it, perpendicular to the wire and rotating accordingly to the direction in which the electric current flows. The magnetic field is highly localized, meaning that the farther you get away from it, the weaker it gets.
To generate a more powerful magnetic field you need a coil, which consists of a wire wrapped so as to create as many loops as possible. The magnetic field can then be concentrated by introducing a material with high magnetic permeability inside the coil, since the maximum magnetic force that can be obtained with the help of an electromagnet depends on the intensity of the magnetic field and on the area of the cross section through the coil.
The intensity of the magnetic field can be calculated by multiplying the relative magnetic permeability of the ferromagnetic material with the number of loops inside the coil and the current intensity through the coil, then dividing that to the length of the coil.