Joel has completely answered the question, but let me add another example, with a bigger failure of injectivity of $g$. Let $D$ be the set of points in the plane of the form $(\frac1n,\sin n)$ for positive integers $n$. Its closure consists of $D$ plus the segment $S=\{0\}\times[-1,1]$ on the $y$-axis. The projection $(x,y)\mapsto x$ is one-to-one on $D$ but its continuous extension to the closure $D\cup S$, given by the same projection formula, is constant on $S$, i.e., on a bigger set than the set $D$ on which it's one-to-one.

There are analogous examples with the segment replaced by any complete, separable, metric space, for example, Hilbert space. And the only reason for needing the word "metric" there is because it was in the question; otherwise, there would be a Stone-Čech compactification example here.