$\newcommand{\dd}{{\rm d}}$
$\newcommand{\vect}[1]{{\bf #1}}$
There are many things going on there
Cosmological principle
In general a metric of the form
$$
\dd s^2 = c^2\dd t^2 - a^2(t) \dd \vect{x}^2 \tag{1}
$$
Satisfies that the cosmological principle: universe is homogeneous an isotropic. In fact, imagine that $t$ is fixed, the effect of $a(t)$ is just "stretching" the coordinates, so if the universe satisfies the cosmological principle at a given time, it will follow it at any time.
Now, if the universe is to be homogeneous, its curvature $\kappa$ must be the same everywhere. There are only a handful of geometries that satisfy this constraint, one is the plane ($\kappa = 0$), other is the sphere ($\kappa = +1$) and finally an hyperbolic universe ($\kappa = -1$). Note that I deliberately used $\kappa = \pm 1$, because the metric can be scaled through the factor $a$ in Eq. (1), so the actual value of the curvature is unimportant.
The most general metric for an universe that follows the cosmological principle is
$$
\dd s^2 = c^2 \dd t^2 -a^2 \left(\frac{1}{1 - \kappa r^2} \dd r^2 + r^2\dd \Omega^2\right) \tag{2}
$$
Einstein's field equations
The equations that describe the coupled evolution of the spacetime geometry and its content are
$$
R_{\mu\nu} -\frac{1}{2}Rg_{\mu\nu} + \Lambda g_{\mu\nu} = \frac{8\pi G}{c^4}T_{\mu\nu} \tag{3}
$$
where $R_{\mu\nu}$ is the Ricci curvature tensor, $R$ the scalar curvature $R = R^\mu_\mu$, $g_{\mu\nu}$ the metric tensor, which can be directly extracted from Eq. (2) $\dd s^2 = g_{\mu\nu}\dd x^\mu \dd x^\nu$, $\Lambda$ the cosmological constant and $T_{\mu\nu}$ the stress-energy tensor which contains information about the physical content of the universe, for example $T^{00} = \rho$
This equation can be then explicitly written in terms of the metric $g_{\mu\nu}$ which results in a relation between the geometry $\kappa$ and the energy content $\rho c^2$ of the universe. The result is what we know as Friedmann equations
$$
\frac{\dot{a}^2 + \kappa c^2}{a^2} = \frac{8 \pi G \rho + \Lambda c^2}{3} \tag{4}
$$
which is the result of calculating the 00 component of Eq. (3). If you take the trace instead, you get
$$
\frac{\ddot{a}}{a} = -\frac{4 \pi G}{3}\left(\rho+\frac{3p}{c^2}\right) + \frac{\Lambda c^2}{3} \tag{5}
$$
Set $\Lambda = 0$ in Eq. (4) and you will conclude about the dynamics of $a$ in terms of the geometry $\kappa$ and the density $\rho$
The fate of the universe
Let us consider for instance a universe for which $\Lambda =0$ and define
$$
\rho_c = \frac{3H^2}{8\pi G} ~~~\mbox{and}~~~ H = \frac{\dot{a}}{a}
$$
You can see that Eq. (4) becomes
$$
1 = \frac{\rho}{\rho_c} - \frac{\kappa c^2}{a^2H^2} \tag{5}
$$
which can be solved for $\kappa$
$$
\kappa = \frac{a^2 H^2}{c^2}\left(\frac{\rho}{\rho_c} - 1\right)
$$
This establishes a clear connection between density and geometry
$\rho > \rho_c$, then $\kappa > 0$ and the universe has spherical geometry (closed)
$\rho < \rho_c$ then $\kappa> 0$ and the universe has hyperbolical geometry (open)
$\rho = \rho_c$ in this case $k = 0$, which corresponds to the Euclidean flat space