How did China become so backwards by the 19th century? I'm talking pre-Opium wars since they are just a symptom of the underlying weakness of Chinese society. What did they do wrong and what can we learn from it?
First off, I would strenuously object to your characterization of China as 'backwards' in this period. Over the past couple thousand years, the predecessor states of modern China have generally been some of the most advanced (if not the absolute most) societies in the world. They dropped from that preeminent position in some degree in the modern era, but to equate that with backwardness is a rather badly misinformed and Eurocentric opinion. For one example, here's a quote from the Qianlong emperor's edict upon the visit of Lord Macartney to the Qing court requesting trade rights:
"impelled by your humble desire to partake of the benefits of our civilization, you have dispatched a mission respectfully bearing your memorial... Our Celestial Empire possesses all things in prolific abundance and lacks no product within its borders. There was therefore no need to import the manufactures of outside barbarians in exchange for our own produce. " http://afe.easia.columbia.edu/ps/china/qianlong_edicts.pdf
This was the emperor's attitude as late as 1793- hardly seems to be the sort of thing you would expect from a backwards or weak nation. There's a certain amount of hubris there, but it doesn't detract from the essential truths. There did happen to be a few areas in which Europe had surpassed China technologically, most notably in terms of military equipment and vessels, which will be important later. However, at this point, it is the British coming in supplication to the Chinese asking to be allowed to expand trade with them.
Now, a more salient point- you mention the "underlying weakness of Chinese society". This is also not really accurate, because those thousands of years of history tend to lead to strong social institutions, but it hits closer to the point. The real question you should be asking is, how did small numbers of Europeans manage to push around the (then as now) most populous nation on earth?
Military, especially naval, technology plays a big role in this, but that's not the whole story. In this period, precisely because China had little use for foreign goods, the Europeans had to pay for their tea, silk, porcelain, and other products with massive amounts of silver, primarily derived from mines in Spanish colonies. This led to inflation, decreasing effective government revenues as well as the salaries of officials, who were then more inclined to turn to graft and corruption (a problem as old as the bureaucracy in China). In addition, Kangxi (Qianlong's grandfather) had frozen the land tax as a mark of his dynasty's prosperity, a policy which his descendants were forced to continue or dishonor their forebear's promise. As if that weren't enough, there had been a huge population boom in the 18th century- but the poll tax had been abolished. So you have a government trying to govern up to twice as many people, with revenues that were actually much less (adjusted for inflation) than a century before. Overall, governance suffered, and the military especially was practically mothballed.
Now, despite this situation, science, technology, and society in China never regressed- they merely failed to advance as fast as the Europeans did, especially in the area of military tech. Thus, with superior naval vessels and facing a weak Chinese military backed by a government that was having enough trouble keeping the country under control, the British and others found it fairly easy to force the Chinese to accept treaties massively favoring the Europeans.
Finally, to answer the question you actually asked, "What did they do wrong and what can we learn from it?", the answers are pretty simple.
Just follow these three crazy tips for governing a nation-state, and you'll never have to worry about massive foreign-government-run drug cartels ever again!
Sources: Currently taking a course on Qing-era China. Primary text: The Search for Modern China, by Jonathan Spence. Also, a wee bit taken from Science and Civilisation in China, by Joseph Needham et al.
China has not existed since 1644.
They lost control of their country. The China that invented the writing system we see, gunpowder and paper were the Han Chinese led dynasties.
The Qing rulers were not Chinese, they were Manchu. All the technology they had were remnants of what the Han Chinese made in the Ming, Song and Tang dynasties.
No technology can develop when your country is being controlled by another group. Even today, some Han Chinese refer to the Manchu as "Northern barbarians"
The reverence of Sun Yat Sen by both the PRC and ROC is a testament to how much the Han resented being ruled by the Manchu.