First assume f is bounded. Adjust the range, so that f(A) fits within (0,1).
Let B be the set of all x in A with f(x) ≤ -1/3, and let C be the set of all x in A with f(x) ≥ 1/3. Since B and C are closed in A, and A is closed, B and C are closed in S. Using Urysohn's lemma, there is a continuous function h1 that is -1/3 on B and 1/3 on C. If you review the construction of this function, you'll note that h1 attains its minimum on B, and its maximum on C. Thus -1/3 ≤ h1(S) ≤ 1/3.
Select an x in A, and it is in B, on its way from B to C, or in C. In each case show that |f(x)-h1(x)| < 2/3. Call this difference j1, and apply the above procedure, substituting j1 for f. This time the image of B is bounded above by -2/9, and the image of C is bounded below by 2/9. This gives a function h2 with h2(S) trapped between -2/9 and 2/9. let j2 = j1-h2, and |j2| < 4/9. Repeat this process, building an infinite sequence of functions.
Let g(x) be the infinite sum over hi(x). Since hi is bounded by a geometric series, g(x) is well defined. Also, each hi is continuous. Use the weierstrass m test to show that convergence is uniform, whence g(S) is continuous.
Remember that h1 is the first approximation to f, and it is off by j1. In the same way, h2 is an approximation to j1, and it is off by j2. So h1+h2 is an approximation to f, that is off by j2. Continue this process and jn is the error term. This approaches 0, hence g(A) = f(A). Thus g is an extension of f.
With |h1| ≤ 1/3, and |h2| ≤ 2/9, and |h3| ≤ 4/27, and so on, evaluate the geometric sum and show that |g| ≤ 1. The bound of g is the bound of f with the endpoints brought in. Let Z be the preimage of ±1 in S. Z and A are disjoint closed sets, and can be separated in disjoint open sets. The topology of Z union A, as a closed set in S, is the topology of Z, and the topology of A, as separate spaces. Define a new function f′ that agrees with f on A, and is equal to 0 on Z. Extend this to a function g′ across all of S. This may also have points that map to 1 or -1, but they will not belong to A or Z. Take the average of g and g′. This is continuous, and agrees with f, and its image lies in (0,1).
Now we can generalize the theorem to an unbounded function f. Set e = f/(1+|f|), so that |e| < 1. Extend e to a function k across all of S as shown above. Then let g = k/(1-|k|). Note that g is continuous, and agrees with f on A.
If A is an open set the theorem fails. Let S be the real line and let A be the complement of 0. Let f(x) = sin(1/x). The image is bounded and continuous, but cannot be extended to x = 0.