Fourier stated that every function can be decomposed into sine and cosine functions. Was he referring to periodic functions only? To a certain class only? I ask, because it seems clear (at least to me) that most functions cannot be so decomposed. One simple example is the n-th degree polynomial. A small portion can be approximated, but the whole function finds no place in the function space spanned by the sines and cosines.
First of all, one has to notice that the work of Fourier predates all modern notions of function. The notion of function was first clarified by Dirichlet, in his attempt to justify Fourier's arguments. What Fourier probably meant was that
a) any reasonable periodic function, say with period $2\pi$ can be expanded into a series of the form $\sum a_k\cos kx+b_k\sin kx$. And
b) that many non-periodic functions on the real line can be represented by Fourier integrals $$\int \cos(sx)\phi(s)ds,\quad\int\sin(sx)\psi(s)ds.$$
Fourier was solving specific problems of mathematical physics. Even his contempories understood that many of his arguments are not mathematically rigorous (and he had difficulty with publication of his book for this reason; it was very much criticized).
He did not specify the exact class of functions for which these statements are true, and it took mathematicians many decades to arrive at exact statements. The very notion of function evolved in this process, beginning with Dirichlet's definition, and proceeding to "$L^2$-functions" (which are not exactly functions in the Dirichlet sense), and then to distributions and hyperfunctions. All these notions were invented with the purpose of making precise sense of Fourier ideas.
To take your specific example of polynomials, only in the 1950s it became clear how to represent polynomials as Fourier integrals; this requires the theory of "tempered distributions".