I have been waiting for 2 hours.
=> the person is speaking right now; have been waiting expresses a chunk of time that started 2 hours ago and continues up until right now.
 I waited for you for 2 hours.
=> The person is no longer waiting. The waiting happened in the past, which is why the simple past tense is used. It tells us that the event (waiting) is no longer going on.
 He had been waiting a long time before he left.
=> Here the person is no longer waiting, so it's like , with the difference that two events are connected in time here:
Event 1: He waited for a long timeIn English, when two events are connected in time, one before the other, the past perfect is used:
Event 2: He left
Event 1: He waited => He had waited, before ...The past perfect takes the form HAVE + past participle (e.g., waited, been).
Event 1: He has been waiting => He had been waiting, before ...