Sources of magnetic field: this can be seen from the Maxwell equations:
$$ \nabla \times \mathbf{H} = \mathbf{J} + \frac{\partial\mathbf{D}}{\partial t} $$
The first term on the right side gives the generation via electrical current and the second term via an oscillating electrical field.
Also, a changing magnetization in space gives rise to a magnetic field:
$$ \nabla \cdot \mathbf{B} = \nabla \cdot (\mu_0 (\mathbf{H}+ \mathbf{M})) = 0 \Rightarrow \nabla \cdot \mathbf{H} = -\nabla \cdot\mathbf{M}$$
Source of magnetization: the magnetization of a material is indeed originating from the atomic dipole moments in the material.
The demagnetization field:
this field is present even if there is no current nor electrical field. The demagnetization field is the field generated by a change in the magnetization. In the bar magnet, the magnetization is assumed to be constant (so no generation of a magnetic field in the bar magnet). Outside the bar magnet, there is no magnetization and thus also no change in magnetization (also no generation of a magnetic field outside the magnet). Now it comes, at the ends of the magnet (the north and south pole), there is a clear and abrupt change of the magnetization (because there is magnetization inside the material but not outside). This abrupt change of the magnetization serves as a source of magnetic field (remember $\nabla \cdot \mathbf{H} = -\nabla \cdot\mathbf{M}$).
At the north pole, the magnetic field is generated (negative gradient of magnetization) whereas at the south pole the magnetic field is eliminated (positive gradient of magnetization). Between the two ends of the bar magnet, there was no change, so the magnetic field should be conserved. Hence, from the maxwell equation, the magnetic field of a bar magnet can be calculated. The field inside is called the demagnetization field and outside is called the stray field. Both fields are going from the north pole to the south pole. The field outside the magnet (the stray field) is the field that we experience and what we use when working with a bar magnet.
Pole avoidance principle:
Indeed, the magnetic field inside the bar magnet will always point against the magnetization. The demagnetization field gives an increase in energy. Hence, nature always strives to minimize the amplitude of this field. This can be done by putting the magnetization along the longest axis of the material.