The Opium War was the result of a long-standing conflict between China and Great Britain over the Opium Trade. Opium had proved a significant source of revenue for Britain's India colony and British merchants were making a significant profit from the (usually) illegal trade in opium to China. The trade had proved so successful that it had reversed the existing trade balance. Rather than having to supply the Chinese silver to pay for porcelain, tea, and silk, the British began receiving silver and those goods for illegal opium.
Obviously, opium did great damage to the Chinese economy as well as the Chinese people. When the Chinese attempted to enforce a ban on the trade, the seized British opium in Chinese ports and provoked a war with the British Empire. The Opium War was devastating to the Chinese. They were many years behind the British in terms of technology, so the British were quickly able to sink of of the Chinese ships and then bombard the Chinese coastal cities at will. The Chinese had no way of fighting back, so they had to surrender and sue for peace.
The terms of the peace treaty were such that the Chinese had to give up their right to dictate market conventions. It also marked the last chance for the Chinese to kick out the Westerners. The treaty ending the war gave the British the perpetual right to have a significant presence in China. Even if China had retained its national sovereignty in the aftermath of the Opium War, however, the war proved that it no longer had the military power to keep out the foreigners.
British victory in the Opium War gave the British a position of strength in which to dictate the terms of the peace. The war proved that the British had a technological and military advantage that the Chinese would not be able to remedy in the foreseeable future. Although China was a vastly larger empire in both land and people than was the tiny island of Britain and the Chinese continued to be the dominant local power in Asia, Britain and other Western countries could not be turned away.
In the unequal treaty ending the Opium War, the British (and subsequent Western powers) used their technological and military advantage to press for increasing accommodations from the Chinese. These unequal treaties resulted in drastic changes in the way China dealt with foreigners. After thousands of years of exercising supremacy in its interactions with outsiders, China found itself being dictated to by those outsiders themselves. Given the technological, economic, industrial, and military advantages enjoyed by Western countries over the Chinese, China had little prospects of reversing those changes. Closing those gaps would require embracing Westernization, which would, itself, make it impossible to go back to the old way of doing things.