Magnetic dipole vs magnetic moment
You ask about magnetic dipole, but you mean magnetic moment due to current loops. Strictly speaking, magnetic dipole is a collection of two magnetic poles with non-zero magnetic dipole moment (the Coulomb magnetic moment). It is a useful model of a magnet from the Coulomb theory of magnetostatics, where magnetic field can do work on magnetic poles; in this theory, force on physical magnetic dipole made of two particles with magnetic pole charge $q,-q$ at positions $\mathbf r_1,\mathbf r_2$ is
$$
\mathbf F = q \bigg(\mathbf H(\mathbf r_1) -\mathbf H(\mathbf r_2) \bigg).
$$
Deriving the formula for potential energy for such a dipole in magnetic field
$$
E_p = - \mathbf m \cdot \mathbf B
$$
is easy, the derivation is just like that for potential energy of an electric dipole in external field in electrostatics.
When you ask how
$$
E_p = - \mathbf m \cdot \mathbf B
$$
is consistent with the idea that magnetic field cannot do work on charged particles (due to the Lorentz formula), you are asking about magnetic moment that is due to a current loop, not about magnetic moment due to magnetic dipole (which belongs to the theory above).
Energy of magnetic moment in external magnetic field
The potential energy formula
$$
E_p = - \mathbf m \cdot \mathbf B
$$
is still valid in a limited sense for systems where the magnetic moment $\mathbf m$ is due to a current loop, but it is harder to derive. It is easier to derive it in special cases, such as magnetic moment due to loop of current.
Work of external forces on a current loop in changing external magnetic field
Consider a planar current loop, placed in an external field $\mathbf B_{ext}$ that we can control. Let the loop be in unstable position in the magnetic field (held in place by some external forces), so that magnetic field is $\mathbf B_{ext,1}$ (rougly in the direction of the $z$-axis), the current is $I_1$ (negative), $\mathbf S = A\mathbf e_z$ is the surface area vector of the circuit, and magnetic moment is $\mathbf m_1 = I_1 \mathbf S = I_1 A \mathbf e_z$ (roughly of opposite orientation to the magnetic field).
Then during a small time interval $\Delta t$ we slowly increase the external magnetic field by a little to a value $\mathbf B_{ext,2} = \mathbf B_{ext,1} + \Delta \mathbf B_{ext}$.
Due to this change, during that time the current will experience induced electric field due to external sources $\mathbf E_{i,ext}$ and entire circuit will experience induced EMF that boosts the current in the original direction. As this proceeds, the external induced electric field will do work on the current, and since this is a conservative system, this work will get stored in the system (as increased magnetic energy). So we can use this work to define potential energy:
$$
\text{increase of potential energy when going from state 1 to state 2} := $$
$$
\text{work of external induced electric force on the current}.\tag{**}
$$
Formally
$$
E_{p,2} - E_{p,1} = \Delta W.
$$
We can express the work done as function of magnetic field and magnetic moment of the current loop. The procedure could be as follows.
Work done during $\Delta t$ can be expressed as the product
$$
\Delta W = \mathscr{E}_{ext} I_1 \Delta t
$$
where $\mathscr{E}_{ext}$ is external induced EMF. This is, due to Faraday's law, equal to minus rate of change of magnetic flux due to external magnetic field through the circuit:
$$
\mathscr{E}_{ext} = - \frac{\Delta\mathbf B_{ext} \cdot \mathbf S}{\Delta t}
$$
Combining the two last equations, we get
$$
\Delta W = - \Delta\mathbf B_{ext} \cdot \mathbf S I_1.
$$
However the circuit has, initially, the magnetic moment
$$
\mathbf m_1 = I_1 \mathbf S.
$$
So the work done is
$$
\Delta W = - \Delta\mathbf B_{ext} \cdot \mathbf m_1.
$$
Defining potential energy for a "frozen" magnetic moment
If the current loop system is such that current (and thus also magnetic moment) stays almost the same during all the changes of external field we do, we can approximate and say the only changing term on the right-hand side of work done (above) is the external magnetic field. This may be the case when there is already a much greater current in the system than the current changes we can cause by the external magnetic field. Then we can define the following potential energy function of the external magnetic field:
$$
E_p (\mathbf B_{ext}) = - \mathbf m \cdot \mathbf B_{ext}. \tag{*}
$$
As long as $\mathbf m$ changes only negligibly during all changes of magnetic field we do, this potential energy obeys (**). This is somewhat applicable to a ferromagnet in a weak external magnetic field. Its magnetization and magnetization current is almost " frozen" and changes only very little due to weak external field.
The correct potential energy for the current loop in perfect conductor
The potential energy derived above is not valid for an ordinary current loop when the current is entirely induced by changes in external magnetic field.
In general, if the magnetic moment changes are partially non-conservative (e.g. the changes of magnetic moment produce some heat inside), then there is no potential energy that can be defined.
However, if our current loop system is conservative (perfect conductor), exact potential energy function can still be derived. We start with the expression for work done during infinitesimal change:
$$
d W = - d\mathbf B_{ext} \cdot \mathbf m.
$$
Now, if both $\mathbf B_{ext}$ and $\mathbf m$ were functions of the same quantity, we could integrate this and find the correct potential energy function.
From Kirchhoff's second circuital law applied to perfect conductor, we have
$$
\mathscr{E}_{ext} + \mathscr{E}_{self} = 0,
$$
$$
\frac{dB}{dt} A = - L\frac{dI}{dt}
$$
where $L$ is self-inductance of the circuit and $B$ is normal component of $\mathbf B_{ext}$ effective in calculating the magnetic flux. From this and the initial condition $I=0, B=0$ we find that magnetic field is a function of current:
$$
B = - \frac{L}{A}I.
$$
Similarly, magnetic moment magnitude is function of current $m = IA$. So now we can express the work done as
$$
dW = d\bigg(\frac{1}{2}L I^2\bigg)
$$
which suggests that potential energy is
$$
E_p = \frac{1}{2}L I^2.
$$
This can be expressed in terms of external field and magnetic moment:
$$
E_p = -\frac{1}{2}\mathbf m \cdot \mathbf B_{ext}.
$$
This energy expression is half of that given by the sought-for formula (*). The different factor is due to fact that during the process of increase of magnetic field, magnetic moment in
$$
- \Delta \mathbf B_{ext} \cdot \mathbf m
$$
is built up from zero. Work on zero or very small magnetic moment as field changes by $\Delta B_{ext}$ is almost zero, but in the end, work on the final magnetic moment is much larger. Magnetic moment is a linear function of the external magnetic field, so net work is one half of the product of the final values.