Proof (Cauchy-Schwarz inequality for expectation)

WLOG assume $X,Y\geq 0$. If one of $\expect(X^2)$ or $\expect(Y^2)$ is $\infty$, then RHS is $\infty$ so the inequality is true.

If one of $\expect(X^2)$ or $\expect(Y^2)$ is 0, say $\expect(X^2)=0$, then $$ \prob(X=0) = 1 \implies \prob(XY=0) = 1 \implies \expect(|XY|) = 0 $$ so the inequality holds.

Hence it suffices to consider the case where $X,Y\geq 0$ and $\expect(X^2), \expect(Y^2)\in(0,\infty)$. Now let $t\in\R$. We have the inequality $$\begin{align*} 0&\leq \big(X - tY\big)^2 \\ 0&\leq \expect\Big(\big(X-tY\big)^2\Big) \tag{*}\\ &=\expect(X^2 - 2tXY + t^2Y^2) \\ 0&\leq \expect(X^2)-2t\expect(XY) + t^2\expect(Y^2). \end{align*}$$ Since this inequality is true for any $t$, the quadratic expression in $t$ has non-positive discriminant: $$ 0\geq 4\expect(XY)^2 -4\expect(X^2)\expect(Y^2) $$ and rearrange to obtain the result.

Equality occurs iff (\*) is an equality: $$ \expect\big((X-tY)^2\big) = 0 \iff \prob(X-tY=0) = 1 \iff \prob(Y=tX) = 1$$