Let $A \subset X$ be a subspace of $X$. Recall that a retraction of $X$ onto $A$ is a continuous map $r: X \to A$ such that $r(a) = a$ for every $a \in A$.
Let $X = \bf R$ endowed with the standard topology, and let $A$ be the closed interval $[0,1]$ endowed with the subspace topology. Clearly, $[0,1]$ is open in $A$, but not in $X$. Am I right to say that, in this case, there is no retraction from $\bf R$ onto $[0,1]$, since $r^{-1}([0,1])$ is closed, and therefore $r$ is discontinuous?
You see in the answers and comments the existence of such a retraction.
I want to get to the point, which you have misunderstood:
Every topological space $Y$ is both, open and closed. And for EVERY function (in particular the continous ones) $f:X \to Y$, it holds that $f^{-1}$ is open and closed. This is because this preimage coincides with $X$. So in your example $r^{-1}([0,1]) = \mathbb R$, which is closed but also open, but not the same as $[0,1]$.
Hope it helps to clean up your confusion.