One interpretation of the question is Hilbert's 17th problem, to characterize the polynomials on $\mathbb{R}^n$ that take non-negative values. The problem is motivated by the nice result, which is not very hard, that a non-negative polynomial in $\mathbb{R}[x]$ (one variable) is a sum of two squares. What is fun about this result is that it establishes an analogy between $\mathbb{C}[x]$, viewed as a quadratic extension by $i$ of the Euclidean domain $\mathbb{R}[x]$; and $\mathbb{Z}[i]$ (the Gaussian integers), viewed as a quadratic extension by $i$ of the Euclidean domain $\mathbb{Z}$. In this analogy, a real linear polynomial is like a prime that is 3 mod 4 that remains a Gaussian prime, while a quadratic irreducible polynomial is like a prime that is not 3 mod 4, which is then not a Gaussian prime. A non-zero integer $n \in \mathbb{Z}$ is a sum of two squares if and only if it is positive and each prime that is 3 mod 4 occurs evenly. Analogously, a polynomial $p \in \mathbb{R}[x]$ is a sum of two squares if and only if some value is positive and each real linear factor occurs evenly. And that is a way of saying that $p$ takes non-negative values.

In dimension 2 and higher, the result does not hold for sums of squares of polynomials. But as the Wikipedia page says, Artin showed that a non-negative polynomial (or rational function) in any number of variables is at least a sum of squares of rational functions.

In general, if $R[i]$ and $R$ are both unique factorization domains, then some of the primes in $R$ have two conjugate (or conjugate and associate) factors in $R[i]$, while other primes in $R$ are still primes in $R[i]$. This always leads to a characterization of elements of $R$ that are sums of two squares. This part actually does apply to the multivariate polynomial ring $R = \mathbb{R}[\vec{x}]$. What no longer holds is the inference that if $p \in R$ has non-negative values, then the non-splitting factors occur evenly. For instance, $x^2+y^2+1$ is a positive polynomial that remains irreducible over $\mathbb{C}$. It is a sum of 3 squares rather than 2 squares; of course you have to work harder to find a polynomial that is not a sum of squares at all.