It doesn’t take very long using a nail gun to have this happen. While shooting something together the nail goes crazy and ends up where you didn’t want it. It can happen at any time but I’ve seen it mainly where the nail follows the grain in a piece of wood or when the nail hits something like another nail. The solution is often to just check you aim, more over a bit and fire again. Then go back and break off the nails below the surface.
There is an easy way to break the nail, like in the above picture, just grab the nail and bend it back and forth usually with pliers. Go with the grain, so on this shelf face in the picture it would be left and right. The nail will work harden, every time it bends it will get harder until it gets brittle and snaps. If you do it right it will snap below the surface and you can deal with the hole.
Another possibility is to drive the nail backwards and pull it out. If it works you have no nail, if it doesn’t you may be back to the first method or it may make the hole fixing more of a problem.
If you have something that needs a finishing nail and it absolutely can’t have a problem (antique brought over on the mayflower) there are other ways. You could go old school and take a regular finishing nail, cut the head off and use it like a drill bit. Drill out the place you want the nail to go and since you cut the nail and put it in a drill it will be shorter than your full nail and will keep it’s holding power but the new nail will follow the hole you drilled regardless of the grain. Then use a nail set to finish off the nail and you are done.
Nail guns are great but every once in a while they will give you trouble. One rule of thumb is to keep your other hand further away than the length of the nail since the nail can bend and take off in any direction. Once I had a nail make a 180 degree turn and go through my thumb. The wierd thing is it didn’t hurt nearly as much as it should have, and it healed quickly.