Weighted L2 space for the Sturm-Liouville problem Let's consider a linear operator
$$
Lu = -\frac{1}{w(x)}\Big(\frac{d}{dx}\Big[p(x)\frac{du}{dx}\Big] + q(x)u\Big)
$$
So the Sturm-Liouville equation can be written as
$$
Lu = \lambda u
$$
Why the proper setting for this problem is the weighted Hilbert space $L^2([a,b], w(x)dx)$?
 A: Because on this space, given appropriate boundary conditions, this operator is self-adjoint (see https://en.wikipedia.org/wiki/Sturm%E2%80%93Liouville_theory#Sturm.E2.80.93Liouville_equations_as_self-adjoint_differential_operators)
A: This has to do with the Lagrange identity:
\begin{align}
                (Lf)g-f(Lg)&= \frac{1}{w}\left[f\frac{d}{dx}\left(p\frac{dg}{dx}\right)-g\frac{d}{dx}\left(p\frac{df}{dx}\right)\right] \\
         &= \frac{1}{w}\left[\frac{d}{dx}\left\{f\left(p\frac{dg}{dx}\right)-g\left(p\frac{df}{dx}\right)\right\}\right] \\
   &= \frac{1}{w}\frac{d}{dx}\left\{ p\left(f\frac{dg}{dx}-g\frac{df}{dx}\right)\right\}
\end{align}
Therefore,
$$
    \left. \int_{a}^{b}\{ (Lf)g-f(Lg) \}wdx =p(fg'-fg')\right|_{a}^{b}       = \left.p\left|\begin{array}{cc}
                     f & g \\ f' & g'
                \end{array}\right|\right|_{a}^{b}
$$
The determinant on the right vanishes at $x=a$, for example, iff there exists $\theta$ such that
$$
      \left[\begin{array}{cc} \cos\theta & \sin\theta \end{array}\right]
                \left[\begin{array}{cc}
                     f & g \\ f' & g'
                \end{array}\right] = 0 \mbox{ at } x=a.
$$
This is why one imposes endpoint conditions such as
$$
               \cos\alpha f(a) + \sin\alpha f'(a) = 0 \\
               \cos\beta f(b) + \sin\beta f'(b) = 0.
$$
These conditions include conditions such as $f(a)=0$, $f'(b)=0$, etc., and all such conditions guarantee that if $f$, $g$ satisfy the same conditions, then
$$
                 \int_{a}^{b}(Lf)g\; wdx = \int_{a}^{b}f (Lg)\; wdx.
$$
It is this "symmetry" of $L$ that guarantees orthogonality of eigenfunctions corresponding to different eigenvalues with respect to the weight function $w$. (The study of symmetric matrices and orthogonal eigenvector analysis came out of these ideas, not the other way around.)
You can see that if $Lf=\lambda f$, $Lg=\mu g$ and if $f,g$ satisfy endpoint conditions as described, then
$$
        (\lambda-\mu)\int_{a}^{b}fg\,wdx=\int_{a}^{b}(Lf)g-f(Lg)\,wdx = 0.
$$
Assuming $\lambda \ne \mu$ forces
$$
                \int_{a}^{b}fg\,wdx = 0.
$$
So you have automatic "orthognality" of eigenfunctions corresponding to  different eigenvalues. But you need that weight 'w' in the integral. It was these identities that motivated the abstract definition of an inner product, which did not follow for another century.
Symmetry of a matrix forces the same thing. Oddly, the study of symmetric matrices and eigenvector analysis came out of these results, not the other way around. The infinite-dimensional came well before the finite-dimensional.
