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.
The intervals as you've defined them won't generally be disjoint. You could try the following: for each $x \in E$ define $a_x = \inf\{a : [a,x] \subset E\}$ and $b_x = \sup\{b : [x,b] \subset E\}$. $a_x$ is possibly $-\infty$ and $b_x$ is possibly $\infty$. Show that 1) if $x \in E$ then $(a_x,b_x) \subset E$, and 2) if $x,y \in E$ then $(a_x,b_x) = (a_y,b_y)$ or $(a_x,b_x) \cap (a_y,b_y) = \emptyset$. Since each open interval contains a rational number there are at most countably many distinct intervals $(a_x,b_x)$, they are pairwise disjoint, and $\displaystyle E = \bigcup_{x \in E} (a_x,b_x)$.
Best Answer
From Walter Rudin's Principles of Mathematical Analysis (third edition, 1976):
Pages 11-12:
Page 31:
It is clear enough, I think, although not made explicit, that in this definition $a$ may be $-\infty$ and $b$ may be $+\infty$.
Page 33:
Here it is undeniable, although again not explicit, that $a, b$ must both be real numbers, i.e. they cannot be $\pm\infty$.
So Rudin is at least a little inconsistent in his use of the term segment.
Because of this very inconsistency, it would be an overstatement to say that Exercise 2.29 is false as it stands. That would be to insist upon one of two possible interpretations of segment. The exercise is merely ambiguous; and common sense must be used to resolve the ambiguity. Almost all indications are that "segments" may be unbounded. One of these indications, of course, is that it is the only interpretation that makes the exercise true.
For what it's worth, it seems most likely to me that 2.21 (g) was an isolated oversight, but I haven't checked the whole book to see if Rudin uses segment consistently everywhere else!