What are magnetic fields?

Simply speaking a magnetic field is the region around a magnet. Magnets are made out of mineral called magnetide (or lodestone) which has the property of attracting iron. Scientists use magnetic field lines to represent these magnetic fields. The magnetic field lines of a simple magnet are shown in the picture below.They point from the north pole to the south pole. However, the magnetic field lines do not just end at the tip of the magnet. They go right through it, so that inside the magnet the magnetic field points from the south pole to the north pole. Thus the magnetic field lines form a closed loop and do not have any ends. Therefore the magnetic field in this case does not have any ends either.
This is actually always true. Magnetic fields, no matter how complicated they are, do not have any ends.

These magnetic fields are very important since they exert an influence on other objects. As we said before, magnets always attract objects made out of iron. In order to see this, put a magnet near a nail (made out of iron) and watch as the magnet pulls the nail toward it? Notice that as you move the magnet closer to the nail the attraction between the nail and the magnet becomes stronger. If you move the magnet further away from the nail the attraction becomes weaker until the moment when the nail stops to be attracted altogether. That's because at some point the nail entered the magnetic field of the magnet and was attracted toward the magnet by the magnetic force. We say that the magnetic field exerts a magnetic force on the nail. The strength of this force depends on the distance between the magnet and the nail.

Magnetic Field Lines.
Unlike Poles Attract Each Other.
Like Poles Repel Each Other. >

However, the magnetic fields of one magnet also exert a magnetic force on other magnets. Take two magnets and place them next to each other. When unlike poles, i.e. the north and the south poles of two magnets are placed close to each other, the magnetic field lines link, causing the magnets to attract each other. If on the other hand, the like poles, i.e. the north and the north pole (or the south and the south pole) are placed close to each other, the magnets do not link and the magnets repel each other.
This is why the needle in a compass always points north/south. The magnetic north pole of the earth attracts the magnetic south pole of the compass needle. Therefore the magnetic south pole of the compass needle points towards the north pole of the earth. If you place a strong magnet next to the compass needle, the needle will turn and point towards the north pole of the magnet. If you remove the magnet the needle will return to its initial position.

However, you do not necessarily need a magnet to obtain a magnetic field. Magnetic fields can also be generated by moving charges (electrons or positively/negatively charged atoms). Thus electrical currents, which are basically just moving electrons flowing along a wire generate a magnetic field. This was discovered by the French physicist Andre Marie Ampere (1775-1836). This effect can be illustrated by a very simple example.

Electrical current generates magnetic field

All you need to perform this experiment is two pieces of wire, a battery, a coil of wire and a compass. Connect the wire and the coil to the battery as shown above. As soon as you do this an electric current starts to flow through the wire and the coil. This electric current generates a magnetic field in the coil as shown in the figure. The magnetic field then makes the needle of the compass turn towards the coil. As soon as you disconnect one of the wires from the battery the compass needle will return to its initial position.