- All electrons have a magnetic moment associated with them, that is just a law of nature that we have to accept..... Even ifthough I had space to detail in basic terms, the math relationship between spin and magneticmangnetic moment, (and if I understood it well enough, which I don't)have taken from Wikipedia Magnetic Moment , you wouldwill still need to accept the notion of magnetic fields as an aspect of nature that currently has no deeper explanation. Although some of the other answers deal far better than I could with explaining electrons in an electric field as a source of magnetism, I wanted to try and answer your question in light of the importance, imo, of magnetic moments and domains in describing magnetism.
The spin magnetic moment is intrinsic for an electron. It is:
$${\displaystyle {\boldsymbol {\mu }}_{\text{s}}=-g_{\text{s}}\mu _{\text{B}}{\frac {\mathbf {S} }{\hbar }}.}$$
Here S is the electron spin angular momentum. The spin g-factor is approximately two: $g_s ≈ 2$. The magnetic moment of an electron is approximately twice what it should be in classical mechanics. The factor of two implies that the electron appears to be twice as effective in producing a magnetic moment as the corresponding classical charged body.
The spin magnetic dipole moment is approximately one $μB$ because $g ≈ 2$ and the electron is a spin one-half particle: $S = ħ/2$.
$${\displaystyle \mu _{\text{S}}\approx 2{\frac {e\hbar }{2m_{\text{e}}}}{\frac {\frac {\hbar }{2}}{\hbar }}=\mu _{\text{B}}.} $$
The z-component of the electron magnetic moment is:
$${\displaystyle ({\boldsymbol {\mu }}_{\text{s}})_{z}=-g_{\text{s}}\mu _{\text{B}}m_{\text{s}}}$$
where $m_s$ is the spin quantum number. Note that $μ$ is a negative constant multiplied by the spin, so the magnetic moment is antiparallel to the spin angular momentum.
The spin g-factor $g_s = 2$ comes from the Dirac equation, a fundamental equation connecting the electron's spin with its electromagnetic properties. Reduction of the Dirac equation for an electron in a magnetic field to its non-relativistic limit yields the Schrödinger equation with a correction term which takes account of the interaction of the electron's intrinsic magnetic moment with the magnetic field giving the correct energy.
For the electron spin, the most accurate value for the spin g-factor has been experimentally determined to have the value
$2.00231930419922 ± (1.5 × 10^{−12}).$
Note that it is only two thousandths larger than the value from Dirac equation. The small correction is known as the anomalous magnetic dipole moment of the electron; it arises from the electron's interaction with virtual photons in quantum electrodynamics. In fact, one famous triumph of the Quantum Electrodynamics theory is the accurate prediction of the electron g-factor. The most accurate value for the electron magnetic moment is
$(−928.476377±0.000023)×10^{−26} J⋅T^{−1}$.