As the comments indicate, the answer truly is that the electrons in the solid are not really free.
But wait, I hear you say -- the free electron model approximates the electrons in the solid as a free gas of electrons. It certainly isn't perfect, but it can't be that poor of a description. Yes it can, and I'll explain why.
Consider what it means to say that a solid is filled with a free electron gas. For definiteness, say that your solid is a metal cube of side $a$. Surely the electrons in the solid are bound to the solid, which is to say, they're not free throughout all of space. They're free inside the solid. So we can model the solid as a 3d infinite square well of width $a$.
But you can never remove a particle from an infinite square well, no matter how much energy you give, via photons or anything else. So it's utterly inadequate as a model for the photoelectric effect. You probably want then something like a very high but finite square well.
If the well is high enough, the finiteness doesn't change the lowest eigenvalues much, which will still be given by
$$E_{n_x,n_y,n_z} \approx \frac{\hbar^2\pi^2}{2ma^2}\left(n_x^2 + n_y^2 + n_z^2\right)$$
Since electrons are fermions, at zero temperature they will occupy the lowest energy eigenstates up to the Fermi energy $E_F$. If we assume that the energy levels are closely spaced enough that $n_x, n_y$ and $n_z$ may be treated as continuous variables, we'll have the following relation for the filled eigenstates:
$$n_x^2 + n_y^2 + n_z^2 \leq \frac{2m}{\hbar^2 \pi^2 } a^2 E_f$$
In other words, the total number of filled eigenstates is approximately given by the volume of the positive octant of a sphere of radius $\frac{a}{\hbar \pi } \sqrt{2m E_f}$.
$$N_F \approx \frac{1}{8}\frac{4}{3} \frac{a^3}{\hbar^3 \pi^2 } (2m E_f)^{3/2}$$
But of course, $N_F$ has to be equal to the number of electrons in the solid (well, up to a factor of two due to spin degeneracy), which is proportional to the volume of the box.
$$C a^3 = 2 N_F \approx \frac{1}{3} \frac{a^3}{\hbar^3 \pi^2 } (2m E_f)^{3/2}$$
So the Fermi energy will be given by
$$E_F = \left(3 \pi^2 C \right)^{2/3}\frac{\hbar^2}{2m} $$
which in accordance with intuition and good sense doesn't depend on $a$. Instead it depends only on $C$ which is a property of the material.
So in order to remove one electron from the material, by whatever means, we need to pay at least the difference between the Fermi energy and the height of the well. This is the work function.
Now I hope it's become clear: the electrons in the box may only be regarded as free as long as their energy is small enough that you can pretend that the well is infinite.
So let's repeat your argument for a photon that has just enough energy to liberate an electron.
\begin{align}
E_{\text{initial}} &= \hbar \omega + mc^2 - W\\
p_{\text{initial}} &= \frac{\hbar \omega}{c}
\end{align}
After the photon is absorbed we have a free electron with zero kinetic energy, so
\begin{align}
E_{\text{final}} &= mc^2\\
p_{\text{final}} &= \frac{\hbar \omega}{c}
\end{align}
Since the condition that the photon has the exact minimum amount of energy to liberate one electron is precisely that $\hbar \omega = W$, the two equations are perfectly consistent and energy is conserved. Of course, since the electron is at rest, it can't have the momentum $p_{\text{final}}$ in the above. So the solid has it, and because it's so much more massive than the electron, its kinetic energy may be neglected. If you cling to the free electron model, I doubt you can go any farther than this.
You might still be feeling uneasy about the whole thing because while the energy imparted to the box is extremely small, it's non zero. That is true. However, the metal is always at some finite temperature so there's always some thermal energy available to supply the minute amount of kinetic energy imparted to the metal. Realistically this would probably be implemented by looking at how the extracted electron scatters off phonons in the lattice or some such.