1. He has taken piano lessons for five years.

2. He has been taking piano lessons for five years.

3. He has worked for three hours on the farm.

4. He has been working for three hours on the farm.

------------------
Are they all grammatical? Which ones are grammatical and commonly used?

All are correct.

1, 2, and 3 mean that he has done the verb's action in the past and continues into the present.

4 means that he started working on the farm at some time in the past and is still doing so ... and hasn't stopped yet!