Edit: This one's been bugging me all weekend. I've even gone so far as to look up perfectly normal.
This property holds for perfectly normal spaces. In a perfectly normal space, every closed set is the zero set of a function (to $\mathbb{R}$, and this characterises perfectly normal spaces according to Wikipedia).
Here's the proof. Let $X$ be a perfectly normal space. Let $U \subseteq X$ be an open set, and $f : U \to \mathbb{R}$ a continuous function. Let $r : X \to \mathbb{R}$ be such that the zero set of $r$ is the complement of $U$. Let $s : \mathbb{R} \to \mathbb{R}$ be the function $s(t) = \min\lbrace 1, |t|^{-1}\rbrace$.
The crucial fact is that if $p : U \to \mathbb{R}$ is a bounded function then the pointwise product $r \cdot p : U \to \mathbb{R}$ (technically, $p$ should be restricted to $U$ here) extends to a continuous function on $X$ by defining it to be zero on $X \setminus U$.
From this, the rest follows easily.
The composition $s \circ f$ is bounded on $U$, hence $r \cdot (s \circ f)$ extends to a continuous function on $X$, say $h$.
The product $(s \circ f) \cdot f$ is also bounded on $U$, since $(s \circ f)(x) = \min\lbrace 1, |f(x)|\rbrace)$. Hence $r \cdot (s \circ f) \cdot f$ extends to a continuous function on $X$, say $g$.
As $s(t) \ne 0$ for all $t \in \mathbb{R}$, $(s \circ f)(x) \ne 0$ for all $x \in X$. Hence $h(x) \ne 0$ for all $x \in U$.
Finally, on $U$, $g(x) = h(x) \cdot f(x)$, whence, as $h$ is never zero on $U$, $f = g/h$ as required.
This isn't a complete characterisation of these spaces. Essentially, this result holds if there are enough continuous functions (as above) on $X$ and if there are too few.
As an example of the latter, consider a topological space $X$ where every pair of non-trivial open sets has non-empty intersection. Then there can be no non-constant functions to $\mathbb{R}$, either on $X$ or on any open subset thereof. Hence every continuous function on an open subset of $X$ trivially extends to the whole of $X$.
However, there's probably some argument that says that once you have sufficient continuous functions (say, if the space is functionally Hausdorff - i.e. continuous functions to $\mathbb{R}$ separate points) then it would have to be perfectly normal. The difficulty I have with making this into a proof is that there's no requirement that the function $g$ be zero on the complement.
Finally, note that metric spaces are perfectly normal so this supersedes my earlier proof. I leave it up, though, in case it's of use to anyone to see the workings as well as the current state. (Actually, for the record I ought to declare that initially I thought that this was false for almost all spaces. However, once I'd examined my counterexample closely, I realised my error and now I'm having difficulty thinking of a reasonable space where it does not hold.)

