I need help understanding the proof of the equality $$E(g(X))=\int^{+\infty}_{-\infty}g(x)f_X(x)dx$$ where $g: \mathbb R \to \mathbb R$ and $X$ is continuous random variable.
The proof on the book:
Suppose that $g$ is a nonnegative function. First, we prove that for the continuous random variable $Y: \Omega \to [0,+\infty)$ it holds $$E(Y)=\int ^{+\infty}_0p(Y \gt y)dy$$ Really, if $f_Y$ is the density for $Y$, then $$\int ^{+\infty}_0p(Y \gt y)dy=\int ^{+\infty}_0 \Biggl(\int ^{+\infty}_yf_Y(x)\Biggr)dy=\int ^{+\infty}_0\Biggl(\int ^x_0dy\Biggr)f_Y(x)dx=\color{red}{\int ^{+\infty}_0xf_Y(x)dx=E(Y)}$$
So for every function $g: \mathbb R \to [0+\infty)$, we have $$E(g(X))=\int ^{+\infty}_0g(x)f_X(x)dx$$
Since every function can be written as the difference of two non-negative function
$$g=g^+-g^-$$ Where $g^+(x)=max{\{0,g(x)}\}$ and $g^-(x)=max{\{0,-g(x)}\}$, then th eproof is done by taking the non-negative function
Now what I don't understand if the red part. Shouldn't the boundaries be $-\infty$ and $+\infty$ for the red equality to be true
I think your confusion stems from a misunderstanding of the structure of the proof. Overall, the proof is establishing an equation for $\mathbb Eg(X)$ that applies to any continuous function $g\colon\mathbb R\to\mathbb R$ and any continuous random variable $X\colon\Omega\to\mathbb R$.
However, the proof starts by solving a different problem: to find a formula for $\mathbb EY$ where $Y\colon\Omega\to\mathbb R$ is a non-negative random variable. This assumption on $Y$ is in place when the "red statement" is made: $$ \int_0^\infty xf_Y(x)\ dx=\mathbb EY. $$
This "red statement" only applies to non-negative random variables, or in other words, random variables satisfying $f_Y(x)=0$ for all $x\leq 0$. So it is obviously correct. But I think your question is more along the lines of why we only need to prove such a statement for non-negative random variables, if the original question does not have such an assumption.
Here is how we do this. Consider $Y=g(X)$, which is a random variable that could be positive and negative - so we can't directly apply the $\mathbb EY$ formula to it. However, using the decomposition you wrote above we can find two non-negative random variables $Y_+$ and $Y_-$, defined such that $Y_+=g_+(X)$ and $Y_-=g_-(X)$. So we only use the formula for $\mathbb EY_+$ and $\mathbb EY_-$, and subtract them to get $\mathbb EY$.
Note. The $\mathbb Eg(X)$ formula has an unrelated problem in the case $\mathbb Eg_+(X)=\mathbb Eg_-(X)=\infty$ (which can occur - just consider two Cauchy random variables, for the positive and negative parts) when it doesn't make sense to subtract the two infinities. The easiest way to avoid this problem is to add the extra assumption that $\mathbb E|g(X)|<\infty$ at the start.