Also, how did it get so bad in the first place?
At the start of the 19th century in Qing China attitudes towards opium were beginning to change. The drug had been present in the region since the 15th century, both home-grown and imported from India in limited quantities, and infused or eaten for effect. Attitudes towards the drug at this time were thus - it was a fancy of the privileged elite and medical and popular opinion could come to no consensus on its effects other than its pain-relieving properties. As far as history tells us, the process of smoking the drug originated in Taiwan in the seventeenth century, spreading to both mainland China and South East Asia and prompting both European and Chinese shipping to China itself from the 1720s. This spread did appear to alarm the Yongzheng Emperor (r. 1722-1735) who made efforts to restrict both opium dens and opium dealing - without going so far as to actually ban the drug. This policy was open to abuse and indeed the growing influence of European powers would contribute to the explosive growth in demand for opium in Qing China from the late 18th century onwards.
Formal European trade in China was established with the arrival of the Portugese in the 16th century - notably the Portugese obtained rights to anchor in the harbours of Macau in 1535 - and over the coming centuries the Portugese hold on the peninsular tightened. The Portugese were soon followed by the arrival of Dutch and British traders [among others], with Macau becoming somewhat of a safe haven for Westerners looking to make money. The Dutch and English had been trading opium in China since the 17th century - Portugese power outside of Macau waned somewhat as the Dutch became the key figures in Chinese trade at this time - but by the 18th century it was British ships which had the biggest foreign influence in Chinese waters.
If Macau was somewhat of a base of operations, the Chinese port of 'Canton' [Guangzhou] was the real prize. As the British East India Company's power, influence and hold on India increased, so too did the desirability of trade in Guangzhou - by the 1770s trade in Guangzhou was the most important to the company on the entire continent. This bustling trade route was driven by the insatiable desire of the British for Chinese tea - 200,000 pounds was sold in London by the Company in 1720, but this number increased to a million by the end of the decade - and as high as nine million in 1770. These huge quantities of tea imported were then re-exported from British shores to colonies across the empire.
As the amount of tea imported increased, a clear problem emerged. The Chinese, largely uninterested in British manufactured goods, [with Emperor Qianlong famously addressing King George III in a letter with the words: "I set no value on objects strange or ingenious, and have no use for your country's manufactures."] would only accept silver in return for the tea, putting the balance of trade overwhelmingly in China's favour throughout the 1700s. The sheer quantities of silver provided to Qing China by the British and other trade companies was of substantial benefit to the Chinese economy in a manner that purveyors of British manufactured goods were all too aware, and born of this was the idea of supplanting this unequal trade relationship with one that British industries could have the upper hand in.
In 1773 the British East India Company took full control of poppy-growing and the production of opium in Bengal, shipping about 60,000 kg annually to China. By 1796 this figure exceeded 200,000 kg, again prompting some Qing response - authorities at Guangzhou banned the Company's opium imports in the same year. The Company technically complied with this order, largely to ensure there was no interruption of vital tea imports to Britain. By this point however, the balance of trade between Britain and the Qing had clearly begun to shift and the Company was determined to continue the exports in exchange for increasingly valuable tea. Thus in reality the Company's opium produce was being offloaded to private syndicates who could be disassociated with it entirely in the event of a challenge by the Chinese, and opium imports at Guangzhou continued to rise. This dramatic increase in availability towards the end of the century prompted a substantial fall in prices - what was once a drug affordable only to the economic elite was becoming available even to the well off peasantry - a trend which did not go unnoticed by the Jiaqing emperor (R. 1796-1821), who enacted harsher penalties to any caught smoking the drug and outright banned all opium imports by any party.
By this point however, it was arguably too late. The British acquisition of the port of Singapore in 1819 proved a perfect staging ground for the smuggling of the drug by ships of various Western countries [largely British and American however] to mainland China where demand amongst the population was entirely unabated by the official Qing stance on the drug. By 1828, 13,000 chests were being imported into China per annum and this number reached 26,000 by 1836. By now opium had become the most valuable single commodity trade in the world. By 1830, bullion was no longer exported to China at all in exchange for tea - the export of opium more than sufficed, and officials in Beijing became concerned largely with its economic impact on China rather than any impending health epidemic it might cause. No longer supplemented by British exports, the value of silver in China began to rise and was associated with a rise in unemployment and urban unrest in the south. Thus at this point the Daoguang emperor (r. 1821-50) sought to canvass opinions on the drug but official opinion still proved divided. Many in official positions saw legalisation as a valid option, with the effect of prohibition clearly at this point inconsequential, and state control could potentially leverage regulation over foreign traders.
It was at this time that British government took control of the trade at Guangzhou directly - cutting out the East India Company entirely, in the interests of its manufactories who had been chomping at the bit for access to potentially lucrative markets within China. The first British superintendent, Lord Napier, arrived in 1834 and represented a key policy change in British interests in the region. As Napier represented a sovereign power and not a mere trading company, he was instructed to conduct negotiations only with those of equal importance - no longer would British trade in the region seek dealings with customs officials but rather the Qing governor-general of the entire southern region. Somewhat ominously, this change in policy and exchange of power was not relayed to the Qing government - and indeed, had it been, it would not have been considered even remotely appropriate.
When the discovery was made Napier was ordered to depart, and, when his refusal was met with Chinese resistance, warships and troops were called for from India. Merchant impatience meant nothing came of this altercation, but the stage for war between Britain and the Qing had been set. This confrontation was perceived as an affront to British interests and would contribute directly to the outbreak of hostilities between Britain and China in 1839 - the First Opium War. By 1838 British opium exports to China reached the millions of kg annually, opium was everywhere in China, Chinese official opinion on legalisation was still split and the Daoguang emperor prepared to lash out at an industry he perceived as troublesome. Appointing official Lin Zexu to the region in 1839 with the mission of eradicating opium imports from foreigners, Canton merchants were pressured later that year and forced to release over 20,000 chests of the drug - which were publicly destroyed by Lin in a proud moment of retribution for Qing officials. Estimates place the reduction of wealth in China at approximately 19 percent by this point, largely due to this unequal trade of opium.
The inability of Qing China to face the British ships which arrived in 1840 was due to a wide variety of issues facing the Qing at the time. The Chinese coastal defenses and traditional bannermen classes were completely ill-prepared for war with troops and ships armed with modern equipment, the seriousness of the war was repeatedly downplayed in correspondence both between officials and with the emperor - to the extent great victories which never materialised were regularly reported to the emperor - Chinese response to British movements was often sluggish and troops often fled their posts at the sight of the enemy - and there were locals unsympathetic with the foreign Manchu ruling class whom assisted the British to varying degrees of success.
And so it was in a couple of years and with seemingly little difficulty British forces were able to enter Nanking and enforce the first unequal treaty on the Qing government - the Treaty of Nanking - giving Britain unrivalled access to Chinese ports and representing a huge loss of control in an already tenuous relationship with the opium trade for the Qing. This opened the floodgates in imperial China to almost a century of Western designs on trade in the country until the dissolution of the empire, no better represented than by the arrival of French and American intervention [alongside British] in a Second Opium War (1856-1860) born of further desires to 'open up' China to western designs and the obvious associated boons in trade. Treaty ports in Qingdao, Hong Kong and elsewhere emerged over the following decades and the legalisation of opium was enforced. Any moral objections in Western governments to the enforcement of a drug trade with an unwilling power were essentially stifled in deference to the huge quantities of money to be made.
Another good read RE: the opium wars (it doubles as a well-written adventure story as well): The Ibis Trilogy by Amitav Ghosh (the first book is called "Sea of Poppies"). It's kind of a sweeping history of the First Opium War, from the colonial powers pushing it, to the sailors trading it, to the farmers and the processing centers, down to the addicts.