Let $L$ be the operator $Lf=-f''$ defined on the domain $\mathcal{D}(L)$ consisting of twice absolutely continuous functions $f$ on $[-\pi,\pi]$ that satisfy periodic conditions $f(-\pi)=f(\pi)$ and $f'(-\pi)=f'(\pi)$. Then $L$ is symmetric on its domain because, for $f,g\in \mathcal{D}(L)$, one has
$$
(Lf,g)-(f,Lg) = \int_{-\pi}^{\pi}f(t)\overline{g''(t)}-f''(t)\overline{g(t)}dt \\
= \int_{-\pi}^{\pi}\frac{d}{dt}\{f(t)\overline{g'(t)}-f'(t)\overline{g(t)}\}dt \\
= \left.\{f(t)\overline{g'(t)}-f'(t)\overline{g(t)}\}\right|_{t=-\pi}^{\pi}=0.
$$
Therefore, if $Lf=\lambda f$ and $f\ne 0$, it follows that $\lambda$ is real because
$$
(\lambda-\overline{\lambda})(f,f)=(Lf,f)-(f,Lf) = 0.
$$
Likewise if $f,g$ are not identically $0$ and $Lf=\lambda f$, $Lg=\mu g$ with $\lambda\ne \mu$, then $(f,g)=0$ because
$$
(\lambda-\mu)(f,g) = (Lf,g)-(f,Lg) = 0.
$$
Note that $L1 = \lambda 1$ where $\lambda=0$. This makes sense because the constant function $1$ is in the domain of $L$, as it is twice absolutely continuous, and it is periodic in the the function and its first derivative. And,
$$
L\sin(nx) = n^2 \sin(nx),\;\; L\cos(nx)=n^2\cos(nx).
$$
So, $\lambda_n = n^2$ are eigenvalues for $n=0,1,2,\cdots$. The eigenspace $\{ 1\}$ for $\lambda=0$ is one-dimensional. The eigenspace $\{\sin(nx),\cos(nx)\}$ is two-dimensional with eigenvalue $\lambda_n=n^2$. There is latitude on how you choose the elements of $E_n$ for $n \ne 0$, but it is convenient to choose an orthogonal basis, which is what choosing $\sin(nx),\cos(nx)$ does. You could instead choose $\{ e^{inx},e^{-inx}\}$, which is also an orthogonal basis. Or you could choose $\{ e^{inx},\cos(nx) \}$, which is not an orthogonal basis for the two-dimensional eigenspace, even though it is a basis.
The standard Fourier basis is $\{ 1,\cos(x),\sin(x),\cos(2x),\sin(2x),\cdots\}$ which consists of orthogonal real functions. To expand $f \in L^2[-\pi,\pi]$ in such a basis,
$$
f \sim a_0 1 + a_1 \cos(x)+ b_1 \sin(x) + a_2 \cos(2x)+ b_2\sin(2x) + \cdots,
$$
one formally takes the dot product of $f$ with one of the basis elements as well as the series on the right. Using orthogonality,
$$
(f,1) = a_0(1,1), \;\;\; a_0 = \frac{(f,1)}{(1,1)} \\
(f,\cos(nx)) = a_n (\cos(nx),\cos(nx)),\;\;\; a_n = \frac{(f,\cos(nx))}{(\cos(nx),\cos(nx))} \\
(f,\sin(nx)) = b_n (\sin(nx),\sin(nx)),\;\;\; b_n = \frac{(f,\sin(nx))}{(\sin(nx),\sin(nx))}.
$$
Using $(1,1)=2\pi$ and $(\cos(nx),\cos(nx))=\pi$, $(\sin(nx),\sin(nx))=\pi$ gives the Fourier series expansion:
$$
f \sim \frac{1}{2\pi}(f,1) + \frac{1}{\pi}\sum_{n=1}^{\infty}\{(f,\cos(nx))\cos(nx)+(f,\sin(nx))\sin(nx)\} \\
\sim \frac{1}{2\pi}\int_{-\pi}^{\pi}f(t)dt
+ \sum_{n=1}^{\infty}\frac{1}{\pi}\int_{-\pi}^{\pi}f(t)\sin(nt)dt\sin(nx)+\frac{1}{\pi}\int_{-\pi}^{\pi}f(t)\cos(nt)dt\cos(nx).
$$
Note: The integral orthogonality did not come from such arguments. The "orthogonality" property of the trigonometric functions was an experimentally discovered fact when trying to write an initial condition for a vibrating string problem in terms of travelling waves. This predated Fourier's work, finite-dimensional linear algebra, eigenfunction analaysis, etc., by decades.
It comes from the Lagrange identity. If you have solutions $y_1$, $y_2$, with eigenvalues $\lambda_1$, $\lambda_2$, respectively, then
$$
((py_1')'+(q+\lambda_1r)y_1)y_2 = 0 \\
y_1((py_2')'+(q+\lambda_2r)y_2)=0
$$
So, if you subtract the second from the first, you get
$$
(py_1')'y_2-y_1(py_2')'=(\lambda_2-\lambda_1)ry_1y_2 \\
\frac{d}{dx}(py_1'y_2-y_1py_2')=(\lambda_2-\lambda_1)ry_1y_2
$$
The last equation is the Lagrange identity. Then, integrating over $[0,1]$ gives
$$
(\lambda_2-\lambda_1)\int_{0}^{1}y_1y_2rdx = p(y_2y_1'-y_2'y_1)|_{0}^{1}.
$$
So, if you have endpoint conditions of the following form for both $y_1,y_2$, then the evaluation terms vanish:
$$
A y_j(a)+B y_j'(a) = 0,\;\; A^2+B^2 \ne 0\\
C y_j(b)+Dy_j'(b) = 0,\;\; C^2+D^2 \ne 0.
$$
And that gives orthogonality of $y_1,y_2$ with respect to the weight function $r$ provided that $\lambda_1\ne\lambda_2$. This is the history of how "orthogonality" with respect to the weight was formulated. This discovery eventually led to the general notion of "inner product" about 70 years later.
Best Answer
For a given $\lambda$, you can solve the equation for all $f$ if and only if $\lambda$ is not one of the eigenvalues $\lambda_n$. The solution as a function of $\lambda$ is holomorphic with single order poles at every eigenvalue. The residue at an eigenvalue $\lambda_n$ is the projection of $f$ onto the eigenvector with eigenvalue $\lambda_n$. The sum of all the residues is the Fourier series for $f$ in the orthogonal eigenvectors $\phi_n$.
Here's an interesting bit of History: These facts led to one of the earliest proofs of the completeness of the eigenfunctions of such an equation. This was done by trading all the residues in the finite plane for one residue at $\infty$, which happens to be $f$; and this must equal the sum of all the residues in the finite plane, which is the Fourier series for $f$ in the eigenvectors of the Sturm-Liouville operator.