A magnetic field is the invisible space around a magnet or an electrical wire where magnetic forces can be exerted on other magnets or on ferromagnetic substances.
The forces between magnets, called attraction and repulsion, can act at a distance through the magnetic field generated by every magnetized object.
Magnetic field lines, sometimes called magnetic lines of force, are used to represent a magnetic field. These lines have a particular orientation, and their spacing reveals the intensity of the magnetic field. Iron filings can be used to see the field lines: the closer the lines are to one another, the stronger the magnetic field. However, this method does not show the orientation of the field lines. To obtain this information, a compass can be placed at different locations around the magnet. Its needle will be deflected in the direction of the field lines, with the north end of the needle pointing to the south pole of the magnet.
The magnetic field lines are always oriented away from the magnetic north pole towards the magnetic south pole. In the example below, the north end of the compass arrow (in black) always points towards the south pole of the magnet.

Magnets with different shapes will produce magnetic fields with different shapes. Magnetic fields also explain the attraction and repulsion that occurs between the poles of different magnets.
The images below show the magnetic field around a bar magnet. In the image on the right, iron filings show the magnetic field around the magnet. The diagram on the left shows a magnetic field represented by field lines, or lines of force. Their spacing reveals the relative strength of the magnetic field: the closer an object is to the magnet, the stronger the magnetic field. These lines are always oriented away from the north pole towards the south pole. They can sometimes come quite close to one another, but they never actually touch.
Even though the orientation cannot be seen, it is still worth noting how the shape of the magnetic field lines compares in the two images: the iron filings are oriented in the same direction as the imaginary magnetic field lines.

The field lines always start from the magnetic north pole and go towards the magnetic south pole. The following diagram shows the same magnet as the previous diagrams but with compasses added to the picture. The compasses are always oriented in the same direction as the nearest magnetic field line and point towards the south pole of the magnet.

The following diagrams show the magnetic field lines around a horseshoe magnet. The magnetic field lines always leave from the magnetic north pole in the direction of the magnetic south pole.

Two different magnetic field shapes can be created when two magnets are brought together. The shape of the magnetic field depends on whether the poles are the same or different.
When two different poles meet, the magnetic field of one magnet is attracted to the magnetic field of the other. The field lines emerging from the north pole of one magnet are attracted to the south pole of the other. This interaction between the opposite poles of two magnets is called attraction. The magnetic field shown below is similar to the field generated by a single magnet.

The shape of the two magnetic fields is identical for two north or two south poles, but the orientation of the field lines is different. The field lines of one magnet are not attracted to the pole of the other magnet, instead they repel each other. This interaction between the like poles of two magnets is called repulsion. The field lines form a characteristic cross in the centre of the magnetic fields.


The Domain Theory of Magnetism
In a given material, there are tiny regions where electrons are aligned. These regions are called magnetic domains. A single magnetic domain can contain several million electrons. Magnetic domains are not always oriented the same way in the same object.
The image below shows the magnetic domains of a regular object on the left and the magnetic domains of a magnet on the right.

Magnetic domains in a regular object

Magnetic domains in a magnet
Clearly, the magnetic domains of the regular object are not aligned. The “mini-magnets” formed by the magnetic domains cancel each other out. As a result, this object does not have a magnetic field.
However, the magnetic domains of the magnet are all aligned in the same direction, and together they form a magnetic force strong enough to establish a magnetic field around the object.