Short answer: when they were defeated in the Opium Wars.
There's a famous story that when presented with a clock, the Chinese saw it as cute trinket and not an ominous portent of a technologically advanced civilisation knocking at their door. The gradual expansion of European trading into the Asian Pacific must have given some Chinese people who had contact with foreigners the impression that those "barbarians" were powerful; but to a dynasty sitting in Beijing, hearing news only filtered through flattering officials, the realisation would have been much much slower due to their assumptions about their position in the world. To illustrate the mindset, here's Jonathan D. Spence in "The Search for Modern China" (2nd edition, 1990):
At [the] root was the assumption that China was the "central" kingdom and that other countries were, by definition, peripheral, removed from the cultural center of the universe. The Chinese, therefore, showed little interest in precise information or detailed study of foreign countries. [...] Chinese descriptions of foreign countries continued to contain an exotic blend of mystical tales and fantasy in which foreigners were often likened to animals or birds and were described in patronizing or deliberately belittling language.
After describing the increasing pressure from European powers to trade and the restrictive reponse of the imperial government, there is the famous 1793 Macartney episode:
Macartney managed to persist in his refusal to prostrate himself full-length on the ground before the emperor in the ritual kowtow, agreeing instead to kneel on one knee and make a series of bows. [...] Unfailingly bland, neither the Qing emperor nor his minister would yield to any of the British requests. Qianlong instead sent an edict to George III explaining that China would not increase its foreign commerce because it needed nothing from other countries. As Qianlong wrote, "We have never valued ingenious articles, nor do we have the slightest need of your country's manufactures. Therefore, O king, as regards your request to send someone to remain at the capital, while it is not in harmony with the regulations of the Celestial Empire we also feel very much that it is of no advantage to your country."
Another embassy sent after Britain had dealt with Napoleon was rudely treated, but "although this episode was used by the British to show that the Qing were unwilling to deal rationally with foreigners, in fact the political complexities of relations with the West were slowly becoming apparent to Qing officials. One indication of this was the growing importance that began to be attached to Canton [where foreigners were allowed to trade] and to the officials who governed the Guangxi-Guangdong region."
All the while, more and more opium was being imported into China by British businesses to pay for the tea they took to Europe. In 1838, Emperor Daoguang decided to stop the opium trade, as it was damaging the Chinese economy - bleeding it of silver money - and had led to widespread addiction. When Chinese officials seized British property and blockaded British subjects in their factories, a fleet was sent to rectify the situation. The result for China was the loss of Hong Kong to Britain and the signing of the "unequal treaties". It was the beginning of the end for the Manchu dynasty and the beginning of a long and painful process where the modern world barged rudely into Chinese history, smashing most of what had been before.
For some of the important events, look up:
Opium Wars
Unequal Treaties / Treaty ports
Taiping Rebellion
Empress Dowager Cixi
Hundred Day's Reform
Boxer Rebellion
1911 Revolution
May 4th Movement
Sino-Japanese War
Great Leap Forward
Cultural Revolution
Reform and Opening
Tiananmen Square Massacre
next stop: who knows...
Edit: spelling, format.