Repeated indices will be summed throughout. Recall that given any two matrices $A = (A_{ij})$ and $B = (B_{ij})$, the matrix product is a new matrix $AB = (C_{ij})$ defined as follows:
\begin{align}
C_{ij} = A_{ik}B_{kj}
\end{align}
In particular, if we think of the first index as the row index and the second index as the column index, then we see that in the matrix product, the column index of the first matrix factor (matrix $A$) is being summed with the row index of the second factor (matrix $B$).
Now, recall that for $A$ given as above, its transpose is defined by $(A^T)_{ij} = A_{ji}$, so we would have
\begin{align}
(A^TB)_{ij} = (A^T)_{ik}B_{kj} = A_{ki}B_{kj}
\end{align}
In other words, when we multiply the transpose of a matrix $A$ with another matrix $B$, in index notation this corresponds to summing over the row indices of both factors $A$ and $B$.
Given these observations, and using the standard convention that the left-most index is the row index (regardless of whether it's up or down) and the right-most index is the column index, we see that in the expression
\begin{align}
(S^{-1})^\mu_{\phantom\mu\rho} g_{\mu\nu}(S^{-1})^\nu_{\phantom\nu\sigma}
\end{align}
the row index of the first $S^{-1}$ factor is being summed with the row index of $g$; so the first two factors involve the product of $(S^{-1})^T$ and $g$.