A constructive and explicit proof proceeds as follows. Since $E$ is closed, $U=\mathbb{R}\setminus E$ is a countable union of disjoint open intervals, say, $U=\bigcup (a_n,b_n)$. Necessarily, we must have that $a_n,b_n\in E$. Define $f(x)$ as follows.
$$ f(x) =
\begin{cases}
g(x) &\text{if }x\in E \\
\frac{x-a_n}{b_n-a_n}g(b_n)+\frac{b_n-x}{b_n-a_n}g(a_n) & \text{if }x\in[a_n,b_n]
\end{cases} $$
Notice first that $f(x)$ is well-defined and also, for all $x\in(a_n,b_n)$, either $g(a_n)\le f(x)\le g(b_n)$ or $g(b_n)\le f(x)\le g(a_n)$ depending on whether $g(a_n)\le g(b_n)$ or otherwise. Clearly, $f$ is continuous on $U$. Now suppose that $x\in E$ and $\epsilon>0$. Then there are a few cases.
Case 1: Suppose that for every $\eta>0$, $(x-\eta,x)\cap E\not=\emptyset$ and $(x,x+\eta))\cap E\not=\emptyset$. Then since $f\vert_E=g$, there is some $\delta>0$ such that if $y\in E$ and $\vert x-y\vert<\delta$ then $\vert f(x)-f(y)\vert<\epsilon$. Because of the condition we have for Case 1, we may choose some $x_1,x_2\in E$ with $x-\delta<x_1<x<x_2<x+\delta$. Choose $\delta'=\min\{x-x_1,x_2-x\}$. If $\vert y-x\vert<\delta'$, then if $y\in E$, we're done. If $y\in U$, then $y\in(a_m,b_m)$ for some $m\in\mathbb{N}$. Furthermore, $a_m,b_m\in E$ and are within $\delta$ of $x$. Also, $f(y)$ is lies between $g(a_m)$ and $g(b_m)$. Thus $f(y)$ is within $\epsilon$ of $f(x)$ since $f(a_m)=g(a_m)$ and $f(b_m)=g(b_m)$ are within $\epsilon$ of $f(x)$.
Case 2: There is some $\eta>0$ for which $(x-\eta,x)\cap E=\emptyset$ or $(x,x+\eta)\cap E=\emptyset$. In this case, $x$ is an endpoint of one of the intervals of $U$. Thus $f$ is linear on either $[x,x+\eta)$ or $(x-\eta,x]$ (maybe both). Certainly, we can get a $\delta>0$ corresponding to $\epsilon$ on this side of $x$. For the other side of $x$, use the argument from Case 1 to get some $\delta'$. Choosing $\delta''=\min\{\delta,\delta'\}$ proves the result.
$\Bbb R\setminus E$ is open, so it can be written as a union of pairwise disjoint open intervals. (Infinite intervals, e.g. $(a,\to)$, are permitted.)
Suppose that $(a,b)$ is one of these intervals; extend $f$ to $(a,b)$ by defining
$$g(x)=\frac{f(b)-f(a)}{b-a}(x-a)+f(a)$$
for $x\in(a,b)$. In other words, just make the graph of $g\upharpoonright[a,b]$ the straight line segment with endpoints $\langle a,f(a)\rangle$ and $\langle b,f(b)\rangle$. If $(a,\to)$ is one of the intervals, let $g(x)=f(a)$ for $x>a$. And if $(\leftarrow,a)$ is one of the intervals, let $g(x)=g(a)$ for $x<a$. The resulting function $g$ clearly has the desired properties.
Best Answer
Take $E = X = \{0,1\}$, and $f(x) = x$. Then there is no continuous extension $g\colon \mathbb{R} \to X$ because $\mathbb{R}$ is connected while $X$ isn't.