$\DeclareMathOperator\Hom{Hom}\DeclareMathOperator\SL{SL}\DeclareMathOperator\PSL{PSL}\DeclareMathOperator\PGL{PGL}\DeclareMathOperator\GL{GL}\DeclareMathOperator\SO{SO}\DeclareMathOperator\R{\mathbb{R}}$Just to set the notation, as in the OP's question, let $S$ be a closed orientable surface of genus $g$ at least 2, and $G$ is a Lie group. The question concerns connected components of $\Hom(\pi_1(S),G)$ with respect to the subspace topology where we identify $\Hom(\pi_1(S),G)$ with a subset of $G^{2g}$ by evaluation. The question in particular concerns the case $G=\SL(n,\mathbb{R})$, however this case is very much related to three other cases, so I will summarize all four cases. We assume $n\geq 2$ everywhere below.
The first paper to mention is:
Goldman, W., Topological components of spaces of representations.
Invent. Math. 93 (1988), no. 3, 557–607.
In that paper, among other foundational results, it is shown that for $G=\SL(2,\mathbb{R})$ there are $2^{2g+1}+2g-3$ components, and for $G=\PSL(2,\mathbb{R})$ there are $4g-3$ components.
Then next foundational paper to mention (as already noted by others) is:
Hitchin, N., Lie groups and Teichmüller space. Topology 31 (1992), no. 3, 449–473.
In that paper, for $G=\PSL(n,\mathbb{R})$ and $n\geq 3$, there are 3 components if $n$ is odd, and 6 if $n$ is even.
Remark: Since $\PSL(n,\mathbb{R})=\SL(n,\mathbb{R})$ when $n$ is odd, this also gives 3 components for $G=\SL(n,\mathbb{R})$ for all odd $n$.
Next, let's mention the paper in Eugene Xia:
Xia, E., Components of $\Hom(\pi_1,\PGL(2,\mathbb{R}))$. Topology 36 (1997), no. 2, 481–499.
In that paper, it is shown there are $2^{2g+1}+4g-5$ components for $G=\PGL(2,\mathbb{R})$.
Generalizing the above result, we have:
Oliveira, A., Representations of surface groups in the projective general linear group. Internat. J. Math. 22 (2011), no. 2, 223–279.
In this paper, it is shown there are $2^{2g+1}+2$ components for $G=\PGL(n,\mathbb{R})$ when $n\geq 4$ and even (which completes the picture for this group given prior results listed above).
Lastly, we mention (out of order):
Bradlow, S.; García-Prada, O.; Gothen, P., Representations of surface groups in the general linear group. Proceedings of the XII Fall Workshop on Geometry and Physics, 83–94, Publ. R. Soc. Mat. Esp., 7, R. Soc. Mat. Esp., Madrid, 2004.
In this paper, it is shown there are $3(2^{2g})$ components when $G=\GL(n,\mathbb{R})$ and $n\geq 3$.
Finally to answer your question. As explained to me by a much more knowledgeable friend (André Oliveira), implicit in the latter two papers is that for $G=\SL(n,\mathbb{R})$ there are $2^{2g+1}+2$ components when $n\geq 4$ and even.
Here what I understood (any errors are my own):
There is only one topological type, determined by the second Stiefel-Whitney class (which takes 2 values). For one value the moduli space is connected. This component contains only representations homotopic to those which factor through $\SO(n)$. For the other value, there is one component like the previous one, but also $2(2^{2g})$ components coming from a choice of a square root of the canonical line bundle for each of the two Teichmüller components in the $\PSL(n,\R)$ case.
Remarks:
- For the $\GL(2,\R)$ case see:
Baraglia, D.; Schaposnik, L., Monodromy of rank 2 twisted Hitchin systems and real character varieties. Trans. Amer. Math. Soc. 370 (2018), no. 8, 5491–5534.
- For $g=1$ and any real reductive $G$, or $g\geq 2$ and $G$ compact or complex reductive, the number of components is equal to the order of $\pi_1([G,G])$. For the $g=1$ case, see Proposition 5.1 (and the references in its proof) in Sikora, A., Character varieties of abelian groups. Math. Z. 277 (2014), no. 1-2, 241–256. For the $g\geq 2$ and $G$ complex reductive (with references for the compact case), see the Appendix of Lawton, S.; Ramras, D., Covering spaces of character varieties. With an appendix by N. Ho and C. Liu. New York J. Math. 21 (2015), 383–416.
- For non-orientable $S$ and $G=\PSL(2,\R)$ or $\PGL(2,\R)$ see Palesi, F., Connected components of spaces of representations of non-orientable surfaces. Comm. Anal. Geom. 18 (2010), no. 1, 195–217. and also Palesi, F., Connected components of PGL(2,R)-representation spaces of non-orientable surfaces. Geometry, topology and dynamics of character varieties, 281–295, Lect. Notes Ser. Inst. Math. Sci. Natl. Univ. Singap., 23, World Sci. Publ., Hackensack, NJ, 2012.