Hong Kong Island was first ceded to Britain in 1842 by China in the Treaty of Nanking. This treaty which is considered to be unequal since it was effectively dictated to the Chinese by the British at the end of the first opium war and heavily favoured them. As part of clause III the Island of Hong Kong was ceded to the British crown in perpetuity.
Clause III
His Majesty the Emperor of China cedes to Her Majesty the Queen of Great Britain, &c., the Island of Hong-Kong, to be possessed in perpetuity by Her Britannic Majesty, her heirs and successors, and to be governed by such laws and regulations as Her Majesty the Queen of Great Britain, &c., shall see fit to direct.
What this implies is that the ceding of the territory is permanent and it is now perpetually under the authority of the British crown. This is similar to the wording of other treaties that ceded territories permanently to Britain such as the treaty of Utrecht in 1713 that ceded Gibraltar. The difference, of course, was that this territorial concession was reinforced by future treaties that reaffirmed it and actual attempts by the Spanish to retake the rock failed which further entrenched the British.
Hong Kong Island only makes up part of the Modern Hong Kong Special Administrative Region (SAR) as it is referred to by the People's Republic of China. The bulk of the land is actually in the New Territories which is a peninsula near Hong Kong island which includes the areas of Kowloon, Tsuen Wan and Sai Kang to name a few. This was leased from China in 1898 under the convention between the UK and China respecting an extension of Hong Kong Territory. The lease would last 100 years and was effectively also dictated to a weaken Qing China in the wake of the first Sino-Japanese War that saw another wave of foreign treaties that took territory from China. Of course, at the time no one anticipated the fallout from the lease only lasting 100 years as politically it was effectively permanent given the political climate and balance of power of the time.
Fast forward to the 1980s and this is a looming issue. Hong Kong had become a crown colony and then later a dependent territory with the British determined to keep it firmly in their hands. Communist China on the other hand for the past 40 years had been pursuing the agenda of reuniting all Chinese territories which included territories held by foreign powers such as Hong Kong, Macau and even Taiwan (although Taiwan deserves its own question). With the lease expiring in 1998 China and Britain entered negotiations to either extend the lease or facilitate a return. At this point, Hong Kong Island and the New Territories had become an integrated urban area making returning the New Territories but not Hong Kong Island logistically impossible. China was also adamant that the ceding of Hong Kong in perpetuity was not valid since it had been dictated to them and emphasized that the New Territories was a lease and they had a legal right to reassume control once it ended. Militarily the defence of Hong Kong against the massive Chinese army was to say the least unfeasible, not to mention the extensive damage and suffering it would inflict on the locals if it did come to blows.
Hence, unable to negotiate an extension of the lease a compromise was made that allowed Hong Kong to retain a separate democratic political system for 50 years after it returned to China.
Sources:
The Anglo-Chinese Treaty of Nanking 1842
Convention of the Extension of Hong Kong Terriotry 1898 -http://treaties.fco.gov.uk/docs/fullnames/pdf/1898/TS0016%20(1898)%20C-9087%201898%209%20JUN,%20PEKING%3B%20CONVENTION%20BETWEEN%20UK%20&%20CHINA,%20RESPECTING%20AN%20EXTENSION%20OF%20HONG%20KONG%20TERRITORY.pdf