The short answer is that China lost Hong Kong to Great Britain in the Opium Wars, and later leased adjacent territories to the British under duress.
Britain's reign over Hong Kong dates back to the 1842 Treaty of Nanking, which ended the First Opium War.
Nineteenth-century Britain had an insatiable appetite for Chinese tea, but the Qing Dynasty and its subjects did not want to buy anything that the British produced. The government of Queen Victoria did not want to use up the country's reserves of gold or silver in buying tea, so it decided to forcibly export opium from the Indian Subcontinent to China. The opium would then be exchanged for tea.
China's government, not too surprisingly, objected to the large-scale importation of narcotics into their country by a foreign power. In 1839, Chinese officials destroyed 20,000 bales of opium. This move provoked Britain to declare war.
The First Opium War lasted from 1839 to 1842. Britain occupied the island of Hong Kong on January 25, 1841, and used it as a military staging point.
China was defeated in the war, and had to cede Hong Kong to Britain in the aforementioned Treaty of Nanking. Hong Kong became a Crown Colony of the British Empire.
Status Changes of Hong Kong, Kowloon and the New Territories
You may be wondering, "Wait a minute, Britain just grabbed Hong Kong. Where did the lease come in, then?"
The British grew increasingly worried about the security of their free port at Hong Kong during the second half of the 19th century. It was an isolated island, surrounded by areas still under Chinese control.
In 1860, at the end of the Second Opium War, the UK gained a perpetual lease over the Kowloon Peninsula, which is the mainland Chinese area just across the strait from Hong Kong Island. This agreement was part of the Convention of Beijing that ended that conflict.
In 1898, the British and Chinese governments signed the Second Convention of Peking, which included a 99-year lease agreement for the islands surrounding Hong Kong, called the "New Territories."
The lease awarded control of more than 200 surrounding small islands to the British. In return, China got a promise that the islands would be returned to it after 99 years.
On December 19, 1984, British Prime Minister Margaret Thatcher and Chinese Premier Zhao Ziyang signed the Sino-British Joint Declaration, in which Britain agreed to return not only the New Territories but also Kowloon and Hong Kong itself when the lease term expired. China promised to implement a "One Country, Two Systems" regime, under which for fifty years Hong Kong citizens could continue to practice capitalism and political freedoms forbidden on the mainland.
So, on July 1, 1997, the lease ended and the government of Great Britain transferred control of Hong Kong and surrounding territories to the People's Republic of China.