I am having trouble understanding this because, on the one hand, it seems like if they didn't want China to have control over Hong Kong, they would just not have ceded the territory in the first place. On the other hand, if they were feeling immense pressure to cede the territory, then why include this provision at all? Why not just give it to China? Did they expect China to become a democracy within those 50 years and want Hong Kong to be protected until that time?
Respectfully, the premise of the question assumes a degree of control, power, and right over Hong Kong and the New Territories which the United Kingdom did not have. If anything, the 50-year transition was a concession from the PRC, not the UK.
It is first important to understand the basis of the UK claim to Hong Kong. The initial cessions, which are enshrined in a set of nineteenth-century treaties from the post-opium war period, covered only the island of Hong Kong (1842) and parts of Kowloon Peninsula (1860). The third region of Hong Kong, called the New Territories, was leased to the British in 1898 for 99 years. When folks from the west think about Hong Kong the city, they're mostly thinking of the Island and Kowloon, which is where Britain's claim was the strongest. But the New Territories contained a substantial amount of Hong Kong's core infrastructure, and so the expiration of the lease in 1997 gave China quite a bit of leverage.
The cession of Hong Kong was not politically neutral in 1972, which is when the PRC joined the UN. In fact, they very quickly sent a letter to the UN special committee on colonialism asserting sovereignty over Hong Kong. Over the next decade, the PRC began laying the groundwork for reclaiming not only the New Territories but all of Hong Kong (and Macau, for what it's worth), culminating in formal talks between the Deng and Thatcher governments in 1982.
The Thatcher government initially wanted to maintain administrative control over HK while ceding sovereignty to the PRC. According to Robert Cottrell, who has written a book on the negotiations, Deng suggested that such an arrangement would simply recapitulate the colonialist history that the PRC was trying to undo. Instead, the PRC proposed that the UK cede all claims to Hong Kong, Kowloon, and the New Territories once the NT lease expired in 1997. Hong Kong would become a "special administrative zone," and the PRC would create policies that ensured HK's "stability and prosperity" (ensuring "stability and prosperity" was language the British used in support of their bid for administrative control). Deng also put a clock on negotiations: if the PRC and UK government did not come to an agreement within two years, China would unilaterally decide the fate of Hong Kong.
Quite honestly, the UK didn't have room to negotiate. Hong Kong Island and Kowloon weren't sustainable without the New Territories, and the UK didn't have a legitimate claim under international law to extend its lease without Chinese consent. Moreover, there wasn't much the UK could do to stop China from simply seizing Hong Kong by force, and a war to preserve a colonial anachronism in the 1980s would have been a difficult proposition, even against the communist government in China. What the UK could do, however, was foment dissent, disorder, and discord in Hong Kong, especially concerning democracy and civil rights. It could also attempt to weaken international trade. It could try to repatriate British businesses or citizens. But doing any of that would come at a significant cost, especially for the UK's substantial finance and trade interests in Hong Kong.
That's where the 50-year transition comes in. As Ming K. Chan writes, "The PRC's design for post-colonial HK as a Special Administrative Region (SAR) under Chinese sovereignty enjoying special rights and autonomy in internal affairs with unchanged legal, economic and social systems for 50 years until 2047 according to the 'one country, two systems' formula in fact accommodated part of the British demands." (Chan, "Different Roads to Home," Journal of Contemporary China 12 (2003): 493-518) Like the British, the PRC did not want Hong Kong to fall apart. The city was an important gateway to western capital and commerce, and it could serve as an important signal to the west that the PRC was a legitimate player in the global economy. They wanted Hong Kong to prosper, but they also wanted it to be a part of China. For better or for worse, I think the 50-year transition was intended to be precisely that, a transition.
From a legal standpoint there is a certain ambiguity to the idea that there is a 50-year expiry date on the Basic Law. Hong Kong's political autonomy following 2047 has always been ambiguous, as the Basic Law itself contains only two references to some form of 50-year cutoff. The first is Article 5:
The socialist system and policies shall not be practised in the Hong Kong Special Administrative Region, and the previous capitalist system and way of life shall remain unchanged for 50 years.
The second is Article 121:
As regards all leases of land granted or renewed where the original leases contain no right of renewal, during the period from 27 May 1985 to 30 June 1997, which extend beyond 30 June 1997 and expire not later than 30 June 2047, the lessee is not required to pay an additional premium as from 1 July 1997, but an annual rent equivalent to 3 per cent of the rateable value of the property at that date, adjusted in step with any changes in the rateable value thereafter, shall be charged.
There is, however, no provision in the Basic Law itself that states that the entirety of the document becomes void on 30 June 2047, and this has been the subject of a number of debates in the legal scholarship. A good if brief summary can be found in Gittings (2011) (full citation below). Theoretically, all that changes is that there is no longer a blanket provision preventing the introduction of 'socialism' (however vaguely defined) and that there are certain lease premiums for pre-handover leases whose waiver cannot be guaranteed past this point.
A critical matter of interpretation is the land lease issue: if, indeed, the Hong Kong Government as an autonomous entity would cease to exist after 2047, then it would not be able to issue land leases that extend beyond that point. However, it has consistently adopted a policy of 50-year leases irrespective of any 2047 cutoff. In other words, from an interpretation standpoint, the sole detailed provision in the Basic Law that references a 50-year cutoff has been understood to apply only to a specific set of pre-handover British-issued leases, and not to fundamentally set an expiry date on the entire Hong Kong governmental structure. Indeed, the existence of any reference to 2047 at all is due to a Chinese concession as part of the 1984 Joint Declaration to allow the British to issue leases extending past the handover with a 50-year cap, not a reference to Hong Kong's future governmental structure and status.
The significance of this is that there almost certainly was no presumption among the British negotiators that Hong Kong's political autonomy would be maintained until 2047 and no further. Rather, the deal that was signed on to guaranteed said autonomy indefinitely, and it was only British land leases that could not be maintained past 2047. That the popular interpretation is that the entire deal expires in 2047 is a different matter entirely, but it doesn't reflect the underlying legal thinking.
If I can ask a follow up, is there another example of a part of a country being stolen as part of colonialism and then after colonialism wanting to be it's own country instead of back to the original country?
I know that Panama was occupied by America from Colombia and never went back to Colombia, but I don't think it was ever actually made part of America like Hong Kong was "legally" (actually, illegally) made part of England