We start by considering the highest power in our expression which is $x^3$. If I want to get rid of that I can start by writing it in terms of $P_3$ and lower powers of $x$.
Solving $P_3$ for $x^3$ gives us,
$$ x^3 = \frac{2}{5} P_3 + \frac{3}{5}x. $$
We thing substitute this for $x^3$ in the expression for $f(x),$
$$f(x) = \frac{2}{5}P_3 + \frac{8}{5} x + 4$$
The next highest power of $x$ that we see is $x^1$. We recognize that we are lucky since this equals $P_1$. If it wasn't we would have solved $P_1$'s equation for $x$ interms of $P_1$ and $x^0$. Making the substitution we get,
$$f(x) = \frac{2}{5}P_3 + \frac{8}{5} P_1 + 4.$$
The next power of $x$ is $x^0$ which also by coincidence happens to be $P_0$ so we make the substitution,
I'll work out a different example involving $x^3$ and $x$.
Suppose we want to rewrite $f(x)=x^3+x+4$ in terms of just Legendre polynomials. We first write down the first four polynomials.
$$P_0 = 1$$ $$P_1=x$$ $$P_2 = (3x^2-1)/2$$ $$P_3 = (5x^3-3x)/2$$
We start by considering the highest power in our expression which is $x^3$. If I want to get rid of that I can start by writing it in terms of $P_3$ and lower powers of $x$.
Solving $P_3$ for $x^3$ gives us,
$$ x^3 = \frac{2}{5} P_3 + \frac{3}{5}x. $$
We thing substitute this for $x^3$ in the expression for $f(x),$
$$f(x) = \frac{2}{5}P_3 + \frac{8}{5} x + 4$$
The next highest power of $x$ that we see is $x^1$. We recognize that we are lucky since this equals $P_1$. If it wasn't we would have solved $P_1$'s equation for $x$ interms of $P_1$ and $x^0$. Making the substitution we get,
$$f(x) = \frac{2}{5}P_3 + \frac{8}{5} P_1 + 4.$$
The next power of $x$ is $x^0$ which also by coincidence happens to be $P_0$ so we make the substitution,
$$f(x) = \frac{2}{5}P_3 + \frac{8}{5} P_1 + 4P_0 .$$
Now we have the Legendre polynomial expansion for $f(x)$.