An element of $\mathbb{R}[x]$ is a sum of two squares if it is nonnegative as a function on $\mathbb{R}$. This can be seen by noting that its real roots have even multiplicity, its irreducible quadratic factors are of the form $(x-a)^2+b^2$, a product of sums of two squares is a sum of two squares, and a square times a sum of two squares is a sum of two squares.
See Qiaochu's question on Hilbert's 17th problemQiaochu's question on Hilbert's 17th problem for what happens in more than one variable.