Why Is an Inhomogenous Magnetic Field Used in the Stern Gerlach Experiment? The apparatus used in the Stern Gerlach experiment is somewhat like the one shown below with magnetic pole pieces of different shapes producing an inhomogeneous magnetic field.
But what would have happened if we had used symmetrical pole peices which produced a near-homogeneous magnetic field? Basically, why not use a homogeneous field?

 A: The potential energy $\text{U}$ of a magnetic dipole moment $\textbf{m}$ in a magnetic field $\textbf{B}$ is
$$\text{U} = - \textbf{m} \cdot \textbf{B}$$
The force on an object in a potential field is in general given by the negative gradient of the potential
$$\textbf{F} = - \nabla\text{U}$$
Which in this case gives
$$\textbf{F} = - \nabla\left(-\textbf{m} \cdot \textbf{B}\right) = \left(\textbf{m}\cdot\nabla\right)\textbf{B} + \textbf{m}\times\left(\nabla\times\textbf{B}\right)$$
Where the cross product term is generally zero unless there's a current density (Ampere's law), and I've already dropped the $\nabla\times\textbf{m}$ term (as it's always zero), leaving $\textbf{F} = \left(\textbf{m}\cdot\nabla\right) \textbf{B}$. For a homogeneous field, $\nabla\cdot\textbf{B} = 0$ everywhere, so there is no force, thus an inhomogeneous field is required to provide a force to separate the atoms according to their dipole moments (spins).
A: The simple answer is that the spins are magnetic dipoles, not monopoles. A monopole will feel a force in any field. But a dipole needs a spatially varying field, because otherwise each pole of the dipole would feel an equal and opposite force (and the net force would be zero).
A: In the Stern-Gerlach experiment you want the atoms
to be deflected depending on the direction of their magnetic dipole moment.
But you get a net force on the magnetic dipole moment
only if the magnetic field is non-homogenous.

(image from "Force and Torque on a Magnetic Dipole" (page 26))
If the magnetic field would be homogenous,
then the forces ($d\vec{F}_1$ and $d\vec{F}_2$ in the image above)
would excactly cancel each other, and the net force would be zero.
A: @Thomas Fritsch uploaded a nice picture that provides intuition about the dynamics of the situation. I would just like to add that the force $\textbf{F}$ exerted on this infinitesimal loop pictured (which is how we model a single silver atom in the Stern-Gerlach experiment), is classically given by:
$$\textbf{F} := \nabla \left( \boldsymbol{\mu} \cdot \textbf{B}\right) \tag{A}$$
where $\boldsymbol{\mu}$ is the silver atom's magnetic dipole moment and is a constant vector (that is, it doesn't exhibit spatial variation). You can easily observe that had $\textbf{B}$ been a constant vector, we would have $$\textbf{F} := \nabla \left( \boldsymbol{\mu} \cdot \textbf{B}\right) \tag{B} = \left(\boldsymbol{\mu} \cdot \nabla\right) \textbf{B} = \mu_x \frac{\partial B_x}{\partial x} + \mu_y \frac{\partial B_y}{\partial y} + \mu_z \frac{\partial B_z}{\partial z} = \textbf{0}$$
Thus, the magnetic field has to be inhomogeneous if we wish to observe a noticeable deflection in the atom's trajectory and thus, investigate the distribution of $\boldsymbol{\mu}$ for an ensemble of silver atoms.

$$\bbox[4px,border:1px solid black]{\textbf{An additional note on Equation $(A)$}}$$
Instead of equation $(A)$, some authors simply write:
$$F_z = \mu_z \left(\frac{\partial B_z}{\partial z}\right) \hat{\textbf{z}}$$
The simplification occurs by the realization that the average force in the $x$ and $y$ directions vanishes, as a result of spin precession around the $\hat{\textbf{z}}$ axis.


(Image taken from page 399, Principles of Quantum Mechanics, Shankar)
A: While the "how" has been well described, the "why" has not been explicitly stated:
The reason for using an inhomogeneous field is so that the device  takes a pure state and entangles the spatial part of the wave function with the spin wave function.
A: Introduction

"But with the outcome of the experiment, I really did not
understand anything..." -Otto Stern(2)

Short answer by Otto:

We sent a beam of silver atoms through a very inhomogeneous magnetic
field. In such a field the magnets are deflected because the field
strength on the place of one pole of the magnet is a little different
from the field strength acting on the other pole. So in the balance a
force is acting on the atom and it is deflected.(9)

@Martin,
well done asking this question! That's the spirit.
A foreword: SGE was performed in times when quantum- and nuclear physics was deriving and refining a lot of thinking and results that where coming from very prominent names. Further, vacuum tech was just evolving to allow for such experimentation.
Dirac in 1929:

The general theory of quantum mechanics is now almost complete...

I try to provide you a slightly different (set of) answer, since I'm not fully pleased with the answers given, because there is more to this important "benchmark"(2) Nobelprize-experiment.
The background is this: Classical physics expected a Larmor precession of the magnetic moments.(2) From (2), let me also quote: "We know today that the directional quantization of angular momentum and magnetic moments in magnetic fields as observed in the Zeeman Effect (Zeeman, 1896, 1897) as well as in the Stern-Gerlach experiment are closely related processes."
Key to understand is that electrons have angular momentum and magnetic momentum.(1) This concept was proposed by Uhlenbeck and Goudsmit.(3)
The highly simplified images of the SG-Experiment crusading the internet avoid taking a closer look at what is really going on with regards to the H-field and atomics. Take a look at the real setup provided by Uni Göttingen and you will notice the magnets arrangement is highly specific in design for creating an inhomogeneous field for this experiment.
This peculiar "gradient field" makes the density of the field a function of location, chiefly ∂Bz/∂z, hence a force is acting. Otherwise, there would be just orientation of the atoms. Note that "m" (= g_s * m_s) takes ~ +-1, so Fz(-m) = -Fz(m) or in words: the expectation that the beam is split into two, not just broadening. The yellow curve (i. e. ionization) as a function of detector position (small coil currents and strong currents).
The yellow curve in the picture was not just broadening, but splitting or from side-view developing two maxima.
Further, this magnetic setup is modelled as two wires with current flow in opposite directions. A near ∂Bz/∂z = const is seeked, which is requisite for later calculating the magnetic momentum: The exact position of the beam in z-axis (z = 0) is calculated (approximated).
Advanced topics: Sebens(4) for example is asking discreteness vs. uniqueness of the spin, and he concludes in "...we need quantum physics to explain the results of the Stern-Gerlach experiment."
I hope this answer leaves your question-answer ratio balanced not split.

Further reading:

*

*Quantenmechanik der Atome. F. Hund. Handbuch der Physik (https://link.springer.com/chapter/10.1007/978-3-642-85687-7_1)

*The Stern-Gerlach Experiment Revisited (https://arxiv.org/abs/1609.09311)

*George Uhlenbeck and the Discovery of Electron Spin (https://doi.org/10.1063/1.881186)

*Particles, Fields, and the Measurement of
Electron Spin (https://arxiv.org/abs/2007.00619)

*Stern-Gerlach: conceptually clean or acceptably vague? (https://arxiv.org/abs/1911.00546)

*Feynman Lectures III - Spin One (https://www.feynmanlectures.caltech.edu/III_05.html)

*Foundations of Potential Theory (https://books.google.de/books?id=AqHyCAAAQBAJ&source=gbs_book_other_versions)

*Molecular Beams in Physics and Chemistry (https://link.springer.com/book/10.1007/978-3-030-63963-1)

*The method of molecular rays, Nobel Lecture, Otto Stern 1946 (https://www.nobelprize.org/uploads/2018/06/stern-lecture.pdf)

