Ian explained it well. The question is who was on his way to work. One way to answer the question is to use "my" or "his". However, rather than use "my", you can also stick with "the" and rearrange the words:
"On the way to work, I met my friend who I hadn't seen for years."
The adjective phrase "on the way to work" modifies "I", not "my friend", because of its position in the sentence. However, if you write
"I met my friend on the way to work who I hadn't seen for years."
the situation becomes more ambiguous. The position in the sentence would tell you it modifies the friend, but the logic leads one to prefer that it modify "I". In such an ambiguous situation it would be best to use "my" or "his".