It's that way because the author wanted it that way. Either, neither, or both of the "cases" could be written without the definite article. That is what the definite article does, it makes a definite reference. Not using the definite article with a plural count noun (such as nails) makes an indefinite reference.
There are 4 places where the author can either use the definite article before nails or not use it:
His father gave him a bag full of A nails. (...) On the first day he hammered in B 37 nails, but getting C nails into the fence was not easy. As the days went by, he was hammering in less nails, and within weeks he was able to refrain from getting angry and from hammering D nails.
You would not use the with less nails because less already makes this an indefinite reference. In any of the other four instances, you can use the if you want to make a definite reference. And not use the when you don't want to make a definite reference.
So the usage depends on what kind of reference the author wants to make. Authors make a definite reference most often when they expect you to know which 'things' they are talking about. Note that this expectation does not have to be correct. And this is not the only times that authors can make a definite reference.
Of course, making a definite reference and an indefinite reference changes how we understand nails, so the meaning of the overall passage will change accordingly.