We had been working for three hours when he came.
He came in the past. We started working three hours before he came and worked continuously until he came. It could all happen years before the uttering of the sentence.
We have been working for three hours.
We started working three hours ago. Now (at the moment when the sentence is uttered) it is three hours after we started working. We worked continuously until now.
Student or Learner