Hong Kong and Macau are former colonies of Britain and Portugal respectively. They were returned to China in 1997 and 1999 respectively. Part of the agreement between the UK, Portugal and China, was an arrangement called, "one country, two systems."
Under the "one country, two systems" deal, China agreed to leave the political and governments of the two regions separate form the mainland for 50 years. It was expected that within that time frame, the three entities politics would evolve into a more homogonous form.
Thus Hong Kong and Macau exist with their own legal systems, laws, taxes, customs duties, imigration policies, police forces, etc. They are virtually like other countries but one country.
At the time that Hong Kong and Macau returned to China, their economic and political situation was quite different from the mainland. Had the border not been there, a great number of mainland people would have migrated to these cities looking for work. The price of goods on either side of the borders is quite different. If goods could move completely freely, then there would have been a collapse in Hong Kong’s pricing structure and money supply.
Something that not many people think about, is that Hong Kong and Macau are both neighbouring Shenzhen and Zhuhai cities. These two cities are Special Economic Zones founded in 1978 and 1980 respectively. These two zones also have separate economic situations from the rest of China. There are restrictions on the movement of goods and also people in and out of these zones. While not as visible as the border with Hong Kong, there is a second border between Shenzhen and the rest of Guangdong province too.
A possible example of a similar, but not really identical, situation that will be familiar to Americans reading this is that of Puerto Rico. Like Hong Kong belongs to China, Puerto Rico belongs to the USA. However, like Hong Kong, Puerto Rico is not a state in the USA.