I have noticed that some literature always define the trace as the sum of the diagonal elements. But sometimes, the trace is defined as $\operatorname{tr}(S) = g^{ab}S_{ab}$.
These two definitions do not always yield the same result. Is it case dependent?
Obviosulsy, for a n-dimensional diagonal matrix S one can find examples where $$S_{11} + S_{22} + \cdots S_{nn} \neq h^{11}S_{11} + h^{22}S_{11} + \cdots h^{nn}S_{nn}. $$
If you're working in a setting where you're distinguishing between upper and lower indices, then it would be better to say that the trace just a matrix contracted with itself -- that is, $S^a{}_a$ or equivalently $S_a{}^a$.
When you have a matrix with two lower indices, you need to raise one of them before you can do that, and introducing the $g^{ab}$ factor is how you do that. In fact in a certain way the main purpose of $g$ is that it should encode how to raise or lower indices.
A matrix with an upper and a lower index corresponds to a linear transformation $V\to V$, and the trace is a property of such a transformation which is invariant of basis changes. This is because the matrix transformation $A\mapsto P^{-1}AP$ for a linear transformation under a basis change preserves the matrix trace.
On the other hand, if you're writing the matrix with two lower (or two upper) indices, then this indicates that you're intending to use it as an bilinear form. If you want to keep a bilinear form the same after a basis change, its matrix representation changes as $A\mapsto P^TAP$. This does potentially change the trace of that matrix, even in dimension 1.
So if you want to have "trace" be a basis-free property of the abstract bilinear form, you need to correct for that. The best we can do is to consider the trace to measure something about the relation between our bilinear form and a fixed inner product on the abstract vector space. The $g$ in $g^{ab}S_{ab}$ represents that fixed inner product in the basis you're working in.
This all doesn't make any difference when the fixed inner product is always the usual $x_1y_1+\cdots+x_ny_n$, represented by the identity matrix -- such as when the basis changes we consider are all orthogonal so they preserve that inner product. Therefore, books that don't put much emphasis on bilinear forms can get away with not mentioning these matters.