Is energy conserved when a nail is hammered? In my book, there is a problem

“A nail placed normal to a wall is struck by a hammer of mass M kg;If the resistant force of the wall is F,what speed(V) should the hammer possess to drive the nail X meters into the wall?”

The problem is solved by equating work done on nail by the wall |FX| with the hammer's kinetic energy (MV2)/2.
My question is: Isn't the collision of the hammer with the nail inelastic? If so, energy is not conserved and the solution is invalid. What would be the correct solution to this problem? And does the mass of the nail (unknown here) come into play?
 A: Problems like this will assume that energy is conserved. In other words, ideal conditions.
Assuming that energy is not conserved, adds an additional level of complexity that is perhaps not suited for the current physics unit you are studying. Maybe later on, things like sound/heat energy losses will also be considered. Also, note that in this question there is no mention of energy loss, so once again it assumes ideal conditions.

Isn't the collision of the hammer with the nail inelastic?

In real life situations, the collision would be inelastic since energy will be lost to sound and heat etc.

What would be the correct solution to this problem?

That is the correct solution to the problem. If you proceed with your studies in physics (engineering etc), there will be problems that do include non-ideal situations.

And does the mass of the nail (unknown here) come into play?

No it need not come into play, since you already have been given the force, and the distance (which is also in the direction of the force) the force moves the nail. The product of these two alone, gives you the work done, and as you mentioned, $$\text{Work Done}=\Delta \text{Kinetic Energy}$$
