How to prove that there exists real polynomials $g(x), h(x)$ such that $f(x)=g^2(x)+h^2(x)$? Prove that a real polynomial $f(x)\geqslant 0, \forall x$ if and only if there exists two real polynomials $g(x), h(x)$ such that $f(x)=g^2(x)+h^2(x)$?

The sufficiency is trivial. 
But my question is how to deal with the necessity. 

Here is another problem that I've solved.
Suppose that real polynomial $f(x)=a_nx^n+\cdots+a_1x+a_0$ with $a_n\neq 0$ has no real root, then there exists two real polynomials $g(x)$ and $h(x)$ such that 
$f(x)=g^2(x)+h^2(x)$.


Hint: Factor out the real roots of $f$ to reduce to the case you've already solved.
A full solution is hidden below.

 Every real root of $f$ must have even multiplicity (otherwise $f$ would change sign at the root).  So we can write $f=a^2b$ for real polynomials $a$ and $b$ where $b$ has no real roots (here $a^2$ is just all the factors from the real roots of $f$).  Then by the problem you say you have already solved, you can write $b=g^2+h^2$, and thus get $$f=(ag)^2+(ah)^2,$$ as desired.

 Just for completeness, here's a proof of the case when $f$ has no real roots.  In that case, all the complex roots of $f$ come in conjugate pairs, so we can factor $$f(x)=c(x-z_1)(x-\overline{z_1})\dots(x-z_n)(x-\overline{z_n})$$ for $c>0$ and non-real complex numbers $z_1,\dots,z_n$.   Let $p(x)=\sqrt{c}(x-z_1)\dots(x-z_n)$, so $f=p\overline{p}$.  Writing $p=g+ih$ where $g$ and $h$ have real coefficients, we then get $f=(g+ih)(g-ih)=g^2+h^2$.

