Why Is an Inhomogenous Magnetic Field Used in the Stern Gerlach Experiment?
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.
@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) \cdot \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 on the same plane.
(Image taken from page 399, Principles of Quantum Mechanics, Shankar)
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).