The magnetic field exerts force on currents and if we place a current loop in a magnetic field, a torque due to the magnetic force on the loop tends to rotate the loop. You may be familiar with DC motors. They work by the same reason of magnetic force on current-carrying conductor and hence on the current loop.
We place a current loop in a uniform magnetic field as shown in Figure 1. The loop carries current \(I\) in counterclockwise direction. The loop shown is rectangular with length \(l\) and breadth \(b\). The plane of the loop is rotated slightly and makes an angle \(\theta\) with the x-axis and therefore the angle between the area vector \(\vec A\) and the magnetic field \(\vec B\) (which is straight up) is also \(\theta\).
In the loop, only the forces \(\vec F_1\) and \(-\vec F_1\) on the lengths contribute to the torque not \(\vec F_2\) and \(-\vec F_2\) on breadths. The breadths make an angle \(90^\circ - \theta\) with the magnetic field and they are along the y-axis in opposite directions, so the force on the breadths cancel to zero (there is no lever arm associated with these forces to create a torque).
You may already know (in magnetic force on a current carrying conductor), that the net magnetic force on a closed loop in a uniform magnetic field is always zero. But the net torque is not always zero.
The forces on the lengths of the loop contribute to the torque on the loop due to the magnetic field. The distance between the line of action of these two forces is \(b\sin \theta\) and the lever arm for each is \(\frac{1}{2}\,b\sin \theta\). So, the net torque on the loop due to each forces on the lengths of the loop is given by
\[\tau = F_1\,b\sin \theta \]
Note that the angle between the length of the loop and the magnetic field is always a right angle. The magnitude of magnetic force on each length is \(F_1 = IlB\), so substituting this value in above equation gives
\[\tau = IlB\,b\sin \theta\]
The product of \(l\) and \(b\) is the area \(A\) of the loop, so
\[\tau = IAB\sin \theta = I \vec A \times \vec B\]
The torque is maximum if \(\theta = 90^\circ\). And it is minimum (zero) when \(\theta = 0^\circ\) or \(\theta = 180^\circ\). The loop is in stable equilibrium position if \(\theta = 0^\circ\) and in unstable equilibrium position if \(\theta = 180^\circ\). The product of \(I\) and \(\vec A\) is called the magnetic dipole moment (or magnetic moment) \(\vec \mu\), and the net torque is
\[\tau = \vec \mu \times \vec B = \mu B \sin \theta\]
The magnetic dipole moment \(\vec \mu\) similar to electric dipole moment \(\vec p\) for an electric dipole has the same direction of area vector \(\vec A\). The direction of \(\vec A\) is determined by the right hand rule. Curl the fingers of your right hand rule in the direction of current in the loop (in this case in anticlockwise direction in Figure 1) keeping your thumb straight up and the thumb gives the direction of \(\vec A\) and \(\vec \mu\).
When the loop is displaced slightly from its equilibrium position, the torque does work on the loop, that is if the torque does work for a small displacement \(d\theta\), the small work \(dW\) is \(\tau\,d\theta\). We have determined the potential energy of an electric dipole, that is \(U_E = -\vec p \cdot \vec E\).
Similarly you can determine the potential energy of magnetic dipole as \(U_B = -\vec \mu \cdot \vec B\). The potential energy is minimum when the angle between \(\vec \mu\) and \(\vec B\) is \(0^\circ\) (\(\vec \mu\) and \(\vec B\) are parallel), that is \(U_B = -\mu\,B\). The potential energy is maximum when the angle is \(180^\circ\) (\(\vec \mu\) and \(\vec B\) are antiparallel), that is \(U_B = +\mu\,B\). And when the angle is \(90^\circ\), the potential energy is zero.