I will try to deal with indefinite integrals. Justifications for the definite integral are more complicated.
Here is the standard argument, somewhat extended. We want to find \int f(g(x)) g'(x)\;dx.
Note that if we can find an antiderivative $F(x)$ of $f(x)$, then one antiderivative of f(g(x)) g'(x) is $F(g(x))$. We can check this by differentiating. For (F(g(x)))'= g'(x)F'(g(x)) by the Chain Rule, since F'(x)=f(x). It follows that \int f(g(x)) g'(x)\;dx=F(g(x))+C.
There was a deliberately unfortunate choice of variable when I wrote "an antiderivative $F(x)$ of $f(x)$." The $x$ here is playing a different role than the $x$ in the original integration question. It would have been better to choose a letter different than $x$, say $u$ for the sake of tradition, or $w$, or $z$, and then to write that we want to find an antiderivative $F(u)$ of $f(u)$. Then our integral is $F(g(x))+C$.
As a shortcut to this, note that the collection of all antiderivatives of $f(u)$ is by definition $\int f(u)\;du$. This is $F(u)+C$. Now substitute $g(x)$ for $u$.
As a shortcut to the shortcut, imagine like before that $u$ is a symbol, but at the same time it is, mysteriously, $g(x)$. Then the substitution step of the previous paragraph is unnecessary, and we simply get \int f(g(x)) g'(x)\;dx =\int f(u)\,du.
How can $u$ be treated as a variable for the sake of the formal manipulation by which we find $\int f(u)\;du$, and simultaneously as the function $g(x)$? One might, as you do, legitimately wonder about this. One point to be made in favour of it is that the above argument proves that the procedure will always give the right answer.
In the integration by substitution process, instead of saying that we will find an antiderivative of $f(u)$ and then substitute $g(x)$ for $u$, we write instead at the beginning "Let $u=g(x)$," then find $\int f(u)\;du$. But it all amounts to the same thing.
It gets more complicated. Soon we will treating $du$ (whatever that means) as an abbreviation for g'(x)\;dx. But we can check that the symbolic manipulations that we do are still the Chain Rule in disguise, and we can certainly always check whether our symbolic manipulations give the right answer.
Comment: The following general idea is useful. Differentiation is ordinarily easy, integration not so much. If after some work one has calculated an indefinite integral, one can quickly check whether the answer is right, by differentiating. This can save us from errors both major and minor. As a simple example, suppose we want $\int e^{-3x}\,dx$. I will write down a wrong answer, $3e^{-3x}+C$. Let's check whether this is right. Differentiate, using the Chain Rule. We get $-9e^{-3x}$. Oops, wrong answer! But we can see how to fix our wrong answer, by dividing that answer by $-9$.