There are a few things going on here. First a function is the data of two sets $A$ and $B$, and an assignment $a\mapsto f(a)\in B$ for every $a\in A$. Having a domain and codomain is very important when dealing with general functions, but people are usually lazy when dealing with functions defined on (most of) $\mathbb{R}$. Given a function $f:A\to B$ and a subset $A'\subset A$, we can restrict $f$ to $A'$. Additionally, if $B\subset B'$, we can compose $f$ with the inclusion $i:B\to B'$ to get a function $i\circ f$. Every function can be written as the composition of a surjective function (namely shrinking the codomain of the function to be the image of the function) followed by an inclusion.
Second, given a function, $f:A\to B$, the inverse is a function $g:B\to A$ such that $f(g(b))=b$ for all $b\in B$ and $g(f(a))=a$ for all $a\in A$. Not every function has an inverse: it must be both injective (if $x\neq y$, then $f(x)\neq f(y)$), and surjective (if $b\in B$, then we can find $a\in A$ with $f(a)=b$). In this case, the inverse is easy to define: if $f(a)=b$, we define $g(b)=a$. Because of surjectivity, we have a value of $g(b)$ for every $b$. Because of injectivity, we have only one value. Therefore, this defines a function.
When we look at $f(x)=x^2$, with $f:\mathbb{R}\to \mathbb{R}$ we see that it is neither injective nor surjective. However, if we restrict both the domain and codomain, and view it as a function $f:\mathbb{R}_+\to \mathbb{R}_+$, then it is both injective and surjective, and so we have an inverse function, namely the square root function.
However, $\sqrt{x}$ is not the only thing that squares to $x$, as $(-\sqrt x)^2=x$. This was why we needed to restrict the squaring function before we could take an inverse: sending $x$ to all things which square to $x$ is NOT a function.
Of course, there are things more general than functions, "multi-valued functions," which take multiple values, and you can construct a normal function from a multi-valued function by choosing one value at every point. In this case, $\sqrt{x}$ and $-\sqrt{x}$ are both branches of a multivalued function defining the inverse to the squaring function. However, multi-valued functions are difficult to work with, and I don't recommend that you give them much thought until you have to (e.g. in a class on complex analysis).