Consider the Hamiltonian
$$\hat{H}=\frac{1}{2}\omega\vec{B}\cdot \vec{\sigma}$$
where $\vec{\sigma}$ is the Pauli vector $=\begin{pmatrix}\sigma_x & \sigma_y & \sigma_z \end{pmatrix}$, $\omega$ is the frequency of the magnetic field $\vec{B}=\begin{pmatrix}B_x & B_y & B_z \end{pmatrix}$
The electron spin is described by a 2 dimensional hilbert space. Suppose we pick the basis of this space as the spin up and spin down states$\{\lvert 1\rangle,\lvert 0\rangle\}$. Then any spin state can be written as follows, with the probability amplitudes $a,b$ subjected to the usual normalisation constraints $$\lvert s(t)\rangle=a(t)\lvert 1\rangle+b(t)\lvert 0\rangle$$
The probability of measuring +1 in some direction $\vec{\sigma}\cdot \hat{n}$, t seconds later can be easily determined by first evolving a given $\lvert s(0)\rangle$ with $e^{-\frac{i}{\hbar}\hat{H}t}$ to get $\lvert s(t)\rangle$ and then compute $||\langle \lambda_+\lvert s(t)\rangle||^2$ where $\lambda_+$ is the eigenvector correspond to +1 for the measurement in the $\hat{n}$ direction. Therefore, we can easily see that what the transformation did by the magnetic field is effectively rotating the spin state.
The magnetic moment of an electron is related to its spin by
$$\vec{\mu}=\frac{ge}{2m}\frac{\hbar}{2}\vec{\sigma}$$
The above calculations however does not seemed to shed any light on how the magnetic moment is influenced by the Hilbert space of the spin state(?)
How do we know (experimentally and theoretically) that the spin must always align with the magnetic moment of the electron (assuming the electron is in a system such that its orbital angular momentum contribution is negligible)?
A brief look at Dirac equation only explain why there is spin (because this arise because when we put quantum mechanics and relativity together, which result in a wavefunction with 4 components), but there is no mention on its relationship with the magnetic moment