Let $X$ and be $Y$ be two metric spaces and $f:X\to Y$ a continuous function. We know that if $A$ is a closed set in $Y$ then $f^{-1}(A)=\{x\in X, \ \ f(x)\in A \}$ is a closed set in $X$.
Now if we have $B$ is a closed set in $X$, does $B$ have the form $B=f^{-1}(A)$ where $A$ is a closed set in $Y$ ?
Multiple examples of when this fails were already given. Let's look at the general question: how could this property hold? First, the only candidate for a set $A$ such that $f^{-1}(A)=B$ is $f(A)$. So, we need $$f^{-1}(f(A))=A$$ for every closed set. Since single points are closed, it follows that $f$ is injective. Let $g$ be the inverse of $f$, which is defined on $f(X)$.
Also, we need $f(A)$ to be closed whenever $A$ is. This is equivalent to $g$ being continuous.
Conclusion: the property you described holds if and only if $f$ is a homeomorphism onto its image. Such a map is called a (topological) embedding of $X$ into $Y$.