Here is Theorem 7.26 by Rudin:
If $f$ is a continuous complex function on $[a, b]$, there exists a sequence of polynomials $P_n$ such that $$ \lim_{n \to \infty} P_n(x) = f(x) $$ uniformly on $[a, b]$. If $f$ is real, the $P_n$ may be taken real.
Here is Rudin's proof:
We may assume, without loss of generality that $[a, b] = [0, 1]$. We may also assume that $f(0) = f(1) = 0$. For if the theorem is proved for this case, consider $$ g(x) = f(x) - f(0) - x [ f(1) - f(0) ] \qquad (0 \leq x \leq 1). $$ Here $g(0) = g(1) = 0$, and if $g$ can be obtained as the limit of a uniformly convergent sequence of polynomials, it is clear that the same is true for $f$, since $f-g$ is a polynomial.
Furthermore, we define $f(x)$ to be zero for $x$ outside $[0, 1]$. Then $f$ is uniformly continuous on the whole line.
We put $$\tag{47} Q_n(x) = c_n \left( 1- x^2 \right)^n \qquad (n = 1, 2, 3, \ldots), $$ where $c_n$ is chosen so that $$ \tag{48} \int_{-1}^1 Q_n(x) \ \mathrm{d} x = 1 \qquad (n = 1, 2, 3, \ldots). $$ We need some information about the order of magnitude of $c_n$. Since $$ \begin{align} \int_{-1}^1 \left( 1-x^2 \right)^n \ \mathrm{d} x = 2 \int_0^1 \left( 1-x^2 \right)^n \ \mathrm{d} x &\geq 2 \int_0^{1/\sqrt{n}} \left( 1-x^2 \right)^n \ \mathrm{d} x \\ &\geq 2 \int_0^{1/\sqrt{n}} \left( 1- n x^2 \right) \ \mathrm{d} x \\ &= \frac{4}{3 \sqrt{n} } \\ &> \frac{1}{ \sqrt{n} }, \end{align} $$ it follows from (48) that $$ \tag{49} c_n < \sqrt{n}. $$
The inequality $\left( 1-x^2 \right)^n \geq 1-nx^2$ which we used above is easily shown to be true by considering the function $$ \left( 1- x^2 \right)^n - 1+nx^2 $$ which is zero at $x= 0$ and whose derivative is positive in $(0, 1)$.
For any $\delta > 0$, (49) implies $$ \tag{50} Q_n(x) \leq \sqrt{n} \left( 1- \delta^2 \right)^n \qquad ( \delta \leq \lvert x \rvert \leq 1), $$ so that $Q_n \to 0$ uniformly in $\delta \leq \lvert x \rvert \leq 1$.
Now set $$ \tag{51} P_n(x) = \int_{-1}^1 f(x+t) Q_n (t) \ \mathrm{d} t \qquad (0 \leq x \leq 1). $$ Our assumptions about $f$ show, by a simple change of variable, that $$ P_n(x) = \int_{-x}^{1-x} f(x+t) Q_n(t) \ \mathrm{d} t = \int_0^1 f(t) Q_n(t-x) \ \mathrm{d} t, $$ and the last integral is clearly a polynomial in $x$. Thus $\left\{ P_n \right\}$ is a sequence of polynomials, which are real if $f$ is real.
Given $\varepsilon > 0$, we choose $\delta > 0$ such that $\lvert y-x \rvert < \delta$ implies $$ \lvert f(y) - f(x) \rvert < \frac{\varepsilon}{2}. $$ Let $M = \sup \lvert f(x) \rvert$. Using (48), (50), and the fact that $Q_n(x) \geq 0$, we see that for $0 \leq x \leq 1$, $$ \begin{align} \left\lvert P_n(x) - f(x) \right\rvert &= \left\lvert \int_{-1}^1 [ f(x+t) - f(x) ] Q_n(t) \ \mathrm{d} t \right\rvert \\ &\leq \int_{-1}^1 \lvert f(x+t) - f(x) \rvert Q_n(t) \ \mathrm{d} t \\ &\leq 2M \int_{-1}^{-\delta} Q_n(t) \ \mathrm{d} t + \frac{\varepsilon}{2} \int_{-\delta}^\delta Q_n(t) \ \mathrm{d} t + 2 M \int_\delta^1 Q_n(t) \ \mathrm{d} t \\ &\leq 4M \sqrt{n} \left( 1 - \delta^2 \right)^n + \frac{\varepsilon}{2} \\ &< \varepsilon \end{align} $$ for all large enough $n$, which proves the theorem.
Why do we assume that $f(0)=f(1)=0$? Also, why do we define the function $g$ and why the $f(x)$ function should be zero for $x$ outside of $[0,1]$ interval? As it's stated in the theorem $f$ is complex function which is continuous on $[a,b]$. So if we assume all these things stated above, we prove the theorem for certain continuous function and not for every case.