Prove that $p \in \mathbb{R}[x]$ can be represented as a sum of squares of polynomials from $\mathbb{R}[x]$
Wlog we can assume that $p$ is monic and let $\deg(p)=2m$. As already noted the roots of $p$ come in conjugate pairs. Choose representatives $z_1, \ldots, z_m$ in each pair. Let $$q(x)=(x-z_1)(x-z_2)\dotsc (x-z_m)\\ \overline{q}(x)=(x-\overline{z}_1)(x-\overline{z}_2)\dotsc (x-\overline{z}_m).$$ Then $p=q\,\overline{q}$. Let $q=a+\textrm{i}\, b$ with $a,b\in \mathbb{R}[x]$. Then $p=(a+\textrm{i}\, b)(a-\textrm{i}\, b)=a^2+b^2$.
Outline: The roots of our polynomial are non-real, and come in complex conjugate pairs. It follows that our polynomial has shape a constant times a product of positive quadratics. Here we have used the Fundamental Theorem of Algebra.
A positive quadratic can be written in the form $(ax+b)^2+c^2$. (Complete the square in the usual way.)
Finally, a product of two sums of two squares is a sum of two squares. This uses the Brahmagupta Identity $$(P^2+Q^2)(R^2+S^2)=(PR+QS)^2+(PS-QR)^2.$$ So a product of an arbitrary number of sums of two squares is a sum of two squares.
We have proved that a polynomial in one variable which is always positive is a sum of two squares.
Remark: We only used a special case of the Brahmagupta Identity. This special case is sometimes (wrongly) attributed to Fibonacci. Diophantus already had used the procedure in his Arithmetica.
Without induction or using an identity, we're looking for two polynomials $A$ and $B$ in $\mathbb R[X]$, such that $P=(A+iB)(A-iB)$.
First, notice that the class of polynomials that can be rewritten as $A^2+B^2=(A+iB)(A-iB)$ is closed under multiplication (use the complex form $(A+iB)(A-iB)$)
Then, rewrite $P$ as a product of irreducible polynomials in $\mathbb R[X]$, that is $$P=\prod_{i=1}^s(X-\alpha_i)^{\omega_i}\prod_{k=0}^t(X^2+2a_kX+b_k)^{\theta_k}$$ where $a_k^2-b_k <0$ and the $\alpha_i$ are distinct.
From the sign assumption on $P$ and the continuity of the polynomial, you can deduce that $\omega_i$ are even.
Also note that $X^2+2a_kX+b_k=(X^2+a_k)^2+\left(\sqrt{b_k-a_k^2}\right)^2$
We have rewritten $P$ as a product of polynomials of the form $A^2+B^2$, we're done.
You can also study two related problems:
A complex polynomial $P$ can be rewritten as $A^2+B^2$ where $A,B$ are complex polynomials.
Replace $\mathbb R$ by $\mathbb Q$ in your original statement, and it stops working.