I was reading a nonstandard analysis textbook, which says that any function $f: \mathbb R \rightarrow \mathbb R$ can be extended to a hyperreal function $f^*: \mathbb R^* \rightarrow \mathbb R^*$ such that $f$ and $f^*$ agree on $\mathbb R$. Does this mean that if we have some element $x$ of $\mathbb R^* \setminus \mathbb R$, then $f^*(x) \notin \mathbb R$?
I have the feeling that this is not the intended meaning of that statement. Is there some way we ensure that $x \in \mathbb R^* \setminus \mathbb R \implies f^*(x) \notin \mathbb R$ ?
This is not the meaning of the phrase. What it means is that if $x\in\mathbb R$, then $f^*(x)=f(x)$. The statement does not say anything when $x\in\mathbb R^*\setminus\mathbb R$, whether $f^*(x)\in\mathbb R$ in that case depends on the specific function $f$ under consideration.
For example, if $f$ is the constant function $f(x)=3$, then $f^*$ is also the constant function $3$ (but now the domain of $f^*$ is $\mathbb R^*$ rather than merely $\mathbb R$); in this case, $f^*(x)=3\in\mathbb R$ for all $x$. On the other hand, if $f(x)=x^2$, then $f^*$ is ("again") the function $f^*(x)=x^2$, but now if $f^*(x)\in\mathbb R$, then $x\in\mathbb R$ as well.
In general, the extension $f^*$ will sometimes take non-real values and sometimes real values on entries from $\mathbb R^*\setminus\mathbb R$. For example, if $f(x)=\sin(x)$, then the range of $f^*$ is the closed interval $[-1,1]^*$, that is, the set of all $x\in\mathbb R^*$ with $-1\le x\le 1$ (so $f$ takes non-real values, for example, every infinitesimal is $f^*(t)$ for some $t$), and given any $n\in\mathbb N^*$, even if infinite, $f^*(n\pi)=0\in\mathbb R$.
Note that if $f$ is continuous on an interval, but not constant there, then $f^*$ takes some non-real values. This is because the image of $f$ contains an interval, but then the image of $f^*$ contains the non-standard version of the same interval. The only way to ensure that $f^*$ only takes real values is to require that the image of $f$ is finite. Otherwise, the image is infinite, so either contains the range of a divergent sequence, and the image of $f^*$ contains infinite elements, or it contains the range of a sequence converging to some real $a$. But then $f^*$ contains points infinitesimally close to $a$.