  The cross section for top quark pair production factorizes at small
transverse momentum of the heavy quark pair, $q_T$. One of the key ingredients
that appears in the factorization formula is the soft function, which mediates
soft gluon exchanges between particles and gives rise to colour correlations.
We present the complete result for the small-$q_T$ soft function at the
next-to-next-to-leading order. This is the last missing element needed to
calculate the NNLO cross section for top quark pair production by means of the
$q_T$ slicing method. In order to evaluate divergent integrals appearing in the
calculation, we develop methods based on sector decomposition and differential
equations. We present an extensive validation of our framework. In particular,
we recover results predicted by the renormalization group, which constitutes a
direct demonstration of validity of the small-$q_T$ factorization at NNLO. We
provide complete results for the real and imaginary part of the soft function,
which are ready for application in the calculation of the $t\bar t$ cross
section at NNLO.
