Prob. 5, Chap. 4 in Baby Rudin: Continuous extension of a function defined on a closed set Here is Prob. 5, Chap. 4 in the book Principles of Mathematical Analysis by Walter Rudin, 3rd edition:

If $f$ is a real continuous function defined on a closed set $E \subset \mathbb{R}^1$, prove that there exist real continuous functions $g$ on $\mathbb{R}^1$ such that $g(x) = f(x)$ for all $x \in E$. (Such functions $g$ are called continuous extensions of $f$ from $E$ to $\mathbb{R}^1$.) Show that the result becomes false if the word "closed" is omitted. Extend the result to vector-valued functions. Hint: Let the graph of $g$ be a straight line on each of the segments which constitute the complement of $E$ (compare Exercise 29, Chap. 2). The result remains true if $\mathbb{R}^1$ is replaced by any metric space, but the proof is not so simple.

First of all, we show that the word "closed" is essential. Let $E = (-\infty, 0) \cup (0, +\infty)$, and let $f \colon  E \to \mathbb{R}^1$ be defined as $$ f(x) = \frac{1}{x} \ \mbox{ for all } x \in E.$$ Then the set $E$ is not closed in $\mathbb{R}^1$, but there is no continuous function $g \colon \mathbb{R}^1 \to \mathbb{R}^1$ such that $g(x) = f(x)$ for all $x \in E$, although $f$ is certainly continuous. Am I right?
Now we show the main result:
First of all, here is Exercise 29, Chap. 2 in Baby Rudin, 3rd edition:

Prove that every open set in $\mathbb{R}^1$ is the union of an at most countable collection of disjoint segments. ...

Since $\mathbb{R}^1 - E$ is open in $\mathbb{R}^1$, it is the union of an at most countable collection of disjoint segments $\left\{ \left(a_n, b_n \right) \right\}_{n \in K}$, where $K$ is either the set $J = \left\{ 1, 2, 3, \ldots \right\}$ or $K$ is some $J_N = \left\{ 1, \ldots, N \right\}$ for some $N \in \mathbb{N}$; moreover, $\left( a_m, b_m \right) \cap \left( a_n, b_n \right) = \emptyset$ for any two distinct $m, n \in K$; finally some $\left( a_n, b_n \right)$ can possibly be infinite.
Now since $$\mathbb{R}^1 - E = \cup_{n \in K} \left( a_n, b_n \right),$$ each of the (finite) endpoints of these segments is an element of set $E$.
Now if some segment $\left( a_n, b_n \right)$ has $-\infty$ as its left endpoint, then we put $g(x) = f \left( b_n \right)$ for all $x \in \left( a_n, b_n \right)$.
If some segment $\left(a_m, b_m \right)$ has $+\infty$ as its right endpoint, then we put $g(x) = f\left( a_m \right)$ for all $x \in \left( a_m, b_m \right)$.
And, for any segment $\left( a_n, b_n \right)$, where $-\infty < a_n < b_n < +\infty$, we put $$ g(x) = f\left( a_n \right) + \left[ \frac{ f\left( b_n \right) - f\left( a_n \right)}{b_n - a_n} \right] \left( x - a_n \right) $$ for all $x \in \left( a_n, b_n \right)$.
I hope I've used the hint given by Rudin correctly. If so, then how to rigorously show that the resulting real function $g$ defined on $\mathbb{R}^1$ is continuous on all of $\mathbb{R}^1$? This is intuitively clear though, but how does the countability of these segments become significant? Up to this point, Rudin hasn't stated any result about the continuity of a function defined using several continuous functions as pieces, like our function $g$ is defined here.
Moreover, are there any other possibilities for $g$ besides the one we have defined above?
Now we state the generalization of the above result for vector-valued functions.

Let $E$ be a closed set in $\mathbb{R}^1$, and let $\mathbf{f}$ be a continuous function defined on $E$ with values in some $\mathbb{R}^k$. Then there are continuous functions $\mathbf{g}$ defined on $\mathbb{R}^1$ with values in the same $\mathbb{R}^k$ such that $\mathbf{g}(x) = \mathbb{f}(x)$ for all $x \in E$.

In order to prove this generalized result, let's put $\mathbb{f}(x) = \left( f_1(x), \ldots, f_k(x) \right)$ for all $x \in E$, where $f_1, \ldots, f_k$ are real continuous functions defined on $E$, and each of these functions we can continuously extend to all of $\mathbb{R}^1$, thereby getting a continuous extension $\mathbf{g}$ of $\mathbf{f}$ from $E$ to all of $\mathbb{R}^1$. Is this reasoning correct?
Last but not the least, I have the following query.

Let $\left( X, d_X \right)$ and $\left( Y, d_Y \right)$ be metric spaces, let $E \subset X$ such that $E$ is closed in $\left( X, d_X \right)$, and let $f$ be a continuous mapping of the induced metric space $E$ into the metric space $Y$. Then can we find---or prove the existence of---a continuous mapping $g$ of $X$ into $Y$ such that $g(x) = f(x)$ for all $x \in E$?
If so, then how to find such a map or prove it exists? If not, then what is the general result that Rudin is referring to and how to come up with the not-so-simple proof that he has alluded to?

 A: The countability of the open intervals comprising the complement of $E$ is not relevant.It is enough that $\mathbb R$ \ $E=\cup J$ where each member of $J$ is $\mathbb R$ or an open half-line or a non-empty bounded open interval, and that the members of $J$ are pair-wise disjoint.
$g$ is continuous at each $x\in \mathbb R$ \ $E$ because $x\in j\in J$ for some $j,$ and j is open, and $g$ is linear on $j.$
For $x\in E$ let   $S=(x_n)_n$ be a sequence converging to $x$:
(1). If $T=\{n: x_n\in E\}$ is infinite, then since $f$ is continuous on $E$ we have $\lim_{n\to\infty\land n\in T}g(x_n)=\lim_{n\to \infty \land x\in T}f(x_n)=f(x)=g(x).$
(2). If $U=\{n:x_n \not \in E\}$ is infinite, let $U^+=\{n\in U:x_n>x\}$ and $U^-=\{n:x_n<x\}.$ 
If $U^+$ is infinite then either 
(i). $x=\inf j$ for some (unique)$j\in J,$ and by the def'n of $g|_{j}$ we have $\lim_{n\to \infty\land n\in U^+}g(x_n)=f(x).$ 
(ii). OR $x_n\in j_n=(a_n,b_n)\in J$ with $b_n<\infty$, for all but finitely many $n\in U^+$  and we will have $b_n\to x$ and $a_n\to x$ .Now $$\min (f(a_n),f(b_n))<g(x_n)<\max (f(a_n),f(b_n)).$$ Since $f(a_n)\to f(x)=g(x)$ and $f(b_n)\to f(x)=g(x)$ we have $g(x_n)\to g(x).$
The case for infinite $U^-$ is handled similarly. 
An exercise in General Topology,by Engelking, in the chapter Metrizable Spaces, is to prove that if $E$ is a closed subset of the metric space $X$ and $f:E\to \mathbb R$ is continuous then $f$ can be extended to a continuous $g:X\to \mathbb R.$ He provides a formula for $g$ as a hint, and the rest is very  easy. And right now I can't recall the formula and I can't find the book. (How can you not find  an 800+ page orange-covered book?)
A: I haven't the time to check your proof right now.  But to address your other questions:


*

*Extension to the case where the codomain is $\mathbb{R}^d$ is immediate, as you say.

*The theorem is not true for two general metric spaces.  Take $X = [0,1]$ with its usual metric and let $E = \{0,1\}$.  Let $Y = \{0,1\}$ with its discrete metric.  Define $f : E \to Y$ by $f(0)=0$, $f(1)=1$.  No continuous extension $g$ can exist because it would map the connected space $[0,1]$ onto the disconnected space $\{0,1\}$.

*The general result which Rudin alludes to is the Tietze extension theorem and you can find its proof in most topology textbooks.
A: Your proof that closedness is necessary is correct.
Your proof of the theorem is correct, and is what I think Rudin intended with the hint. The countability of the segments is not important and can be ignored in the proof if you are willing to index the interval with an arbitrary ordinal.
To prove that it is correct, one reasons as follows: a function is continuous if it is continuous at every point. Continuity at a point is a local property which means that it only takes into account a small neighborhood of the point in question (in contrast, "the $x$ such that $f(x)$ takes on the maximum value" is not a local property). If $x$ is in the domain of the original function and not at an exterior point, then in a small enough neighborhood of $x$, $f=g$. Thus $g$ is continuous at those points because $f$ is. Similarly, on the complement of the region on which $f$ is defined, we find out that on a small enough interval, $g$ agrees with a linear function, and linear functions are known to be continuous. Thus $g$ is continuous everywhere except possibly at the boundary of the region at which $f$ is defined. To show that $g$ is continuous at these points, compute the left and right limits separately, and then see that they agree and agree with the actual value of $g$.
One thing you'll notice about this proof is that the only thing that matters about the linear function is that it is continuous and has limits that make $g$ continuous at the exterior of the region on which $f$ is defined. In fact, if you take any continuous function that takes on the same values as $f$ on said regions, you can use those to stitch together $f$. If you draw a picture, it should be visually clear that you can take your $g$ and introduce whatever wiggles you wish into the straight lines without violating continuity, as long as you keep the wiggles themselves as being continuous.
The rest of your question has been already answered.
Your reasoning about $\mathbb{R}^n$ is correct.
