How and why a magnetic field is produced?
Why is a magnetic material magnetic?
- All electrons have a magnetic moment associated with them, that is just a law of nature that we have to accept..... Even though I detail in basic terms, the math relationship between spin and mangnetic moment, which I have taken from Wikipedia Magnetic Moment , you will 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}$.
From Magnetic Domains Wikipedia
Moving domain walls in a grain of silicon steel caused by an increasing external magnetic field in the "downward" direction, observed in a Kerr microscope. White areas are domains with magnetization directed up, dark areas are domains with magnetization directed down.
Some materials, such as iron, allow these electrons to clump in organised domains, rather than randomly, which lets iron's molecular structure act to enhance the magnetic effect, so all these magnetic moments are aligned in the same direction.
The domains have to occupy a relatively large volume of the material, depending on their strength. Aluminum is an example of a metal in which the domains exist but are not as organized or as widespread as iron.
Another example of how important the magnetic domains are, is the fact that almost all magnetic materials are solids. In fact melting a solid will normally disrupt its magnetic field, again because domains are disrupted and again iron is special, as the liquid core of the Earth still makes a compass needle move.
Mostly responsible for a magnetic field are electrons.
Electrons magnetic dipole moment and unpaired electrons
In atom only two electrons can share the same the same quantum state except their intrinsic magnetic dipole moment. This moments have to be oriented in opposite directions. Thus the opposite directions makes the atom more or less magnetically neutral. But there exist atoms with unpaired electrons and this atoms have a remarkable magnetic moment. (To draw the full picture, if one influent atoms strongly the magnetic dipole moments of all subatomic particles will be observable.)
Alignment of the electrons magnetic fields
From a remarkable magnetic dipole moments in atomaric level it couldn't concluded that in the macroscopic level a magnetic field would exist. There are some natural minerals there the magnetic dipole moments of the involved electrons are aligned and thus the act as magnets. To make a more strong magnet the powder of magnetic materials is pressed and by this influenced with an external magnetic field which aligns the powders magnetic fields to a common field.
As you can see, responsible for the macroscopic phenomenon of magnetic field is the intrinsic (under all circumstancess existing) property of magnetic dipole moment of the involved particles.