Hmm, 了 is indeed a big trouble.
My feeling is a bit different from the previous answers. The following is my personal understanding.
To me, the final 了 modifies the whole action and marks
perfect aspect, and the first 了 marks
the length of the time that the action has been carried out (instead of the usual simple usage that marks action is past/completed/etc). And I would group the words as 我在那儿(住了两年)了, where "住了两年" corresponds to "was living for 2 years/lived for 2 years", and the final "了" carries the sense of "have done".
我住了两年

我(住了两年)了

(the first 了 marks the action was carried out for two years)
我搬了三次家

我(搬了三次家)了

(the first 了 marks the action was repeated for three times)
他去了纽约

他(去了纽约)了

(the first 了 marks "perfective aspect", i.e. the action is a whole and in contrast to ongoing/habitual/repeated/etc, not to be confused with the above "perfect aspect" which is the function of the final 了)
I thought the final 了 might be related to interjections. A final 啦/喽 can have a similar function as 了, the difference is that 啦/喽 also carries some emotion. In classic Chinese, a final 矣 feels like a word with similar function. "Blah blah blah 啦!" is like saying "Blah blah blah DONE!". That is, the interjection emphasizes the action has been done and focuses on the result, effect or how it is related to what we are talking about.
However, if we rephrase the sentence a little bit,
我(居)住在那儿

我(居)住在那儿/两年了

我在那儿居住

我在那儿居住/两年了

(BTW, I don't know why 我在那儿住两年了

sounds awkward to me. So I didn't include it here.)
then the sentences would feel quite different, and I would agree to group (两年了) together, and as you see the part before 两年了 makes perfect sense (in the previous sentences with two 了s, the part before the final 了 makes perfect sense.)
Anyway, since the OP's question is about the second 了, the answer can be simple. In #1 and #2, the sentence can both be translated as "I've been living there for 2 years/I have lived there for 2 years". The final 了 indicates perfect aspect, i.e. it is "focusing attention on the resulting state rather than on the occurrence itself" (words from Wikipedia). What is the result of 住了两年? It may be "two years is too long", it may also be "I'm still staying there", depending on the context.