let us say the question is about an electric dipole or about a magnetic dipole. It is very similar for magnetic or electric dipole.
Let an electric dipole of charges +q at point A and -q at point B be attached by a thin weightless dielectric rod AB of length 2a. The electric dipole moment is 2 a q = p. The thin rod id held fixed at a pivot O in the center of AB. It can rotate freely in the plane of electric field E and the dipole axis.
Let there be an electric field E uniform in that region. Let the electric dipole line joining them make an angle with the electric field E. Then there is a force F on +q in the direction of E, and F = + qE. There is a force on the charge -q (coulomb's force) F2 = - F = -q E.
Since these two forces are not collinear, they do not cancel. There is a torque around the fixed point O. Because F and -F try to rotate the rod AB in the same direction.
magnitude of Torque = sum of arm length * force
= a Sin θ * q E + a Sin θ * q E
= 2 a q E Sin θ = p E Sin θ
In vector notation, we have
T = Sigma r X F = a q E Sin θ k + (- a) ( - q E) k
= 2 a q E Sin θ k = p X E
The dipole and field are in x-y plane and torque is perpendicular to them is parallel to the z axis, passing through O.