I know it works, but I'm trying to make sense of why the domain variable is also swapped. I know that one-to-one functions have to pass the horizontal test and I know that in order to do that, with some functions, you have to specify a domain. For example, for the function $y = x^2$ in order to ensure that the inverse function is a function, we have to limit the domain to be something like $x > 0$
Here is the text:
As you can see, the inverse function limits $y \le 0$. But why? How was this more formally established?

If $f\colon A\longrightarrow B$ is injective, its inverse will be a function from $f(A)$ into $A$.
In the given example, $f$ is a function from $(-\infty,0]$ into $\mathbb R$. Besides, the image of $f$ is $[0,+\infty)$. Therefore, the inverse is a map from $[0,+\infty)$ onto $(-\infty,0]$.