I know that
$\int_{a}^{b}g(x)dx = (b-a)\int_{a}^{b}g(x)\frac{1}{b-a}dx = (b-a)E[g(X)]$
where $X$ has a $Uniform(a,b)$ distribution.
However, I don't understand how the second step turns the integral into an expected value. It's as if I can simply "declare" that $X$ is a random variable. Somehow, it seems like I'm hand-waving some magic into the equation, and I wonder if I am missing a step.
As mentioned in the comments, this works because it follows from the definition of expectation of a continuous random variable.
Consider a continuous random variable $X$ defined on some interval $I$ with some distribution with pdf $f_X(x)$. Then,
$$\Bbb E[g(X)]:=\int_I g(x)f_X(x)~\mathrm dx$$
The question here corresponds to $X\sim\mathrm{Unif}(a,b)$ for which $f_X(x)=\frac 1{b-a}$ for $x\in [a,b]$ and $0$ otherwise, so we have,
$$\Bbb E[X]:=\int_a^b g(x)\frac 1{b-a}~\mathrm dx\implies (b-a)\Bbb E[X]=\int_a^b g(x)~\mathrm dx$$