"Why" is often a difficult question. However, we can note two things that did contribute.
First, Western gunpowder weapons only passed those of China from about the mid-15th century. From the mid-16th century, the Chinese began adopting the Western advances that had been made (and often making improvements). For example, matchlock muskets became standard in the mid-16th century, and European cannons were copied and improved in the late-16th century (folangji, "Frankish cannon"), and culverins were copied and produced in large numbers in the mid-17th century (hongyipao, "red barbarian cannon"). Was there anything special about the period c. 1450-1550? It was relatively peaceful. While far from free of both internal and external war, it was peaceful compared to the previous many centuries - centuries of war between the Tanguts/Xi Xia, Khitans/Liao, Jurchens/Jin, Mongols, and Song, followed by civil war with the end of Yuan/Mongol rule, and expansion during the early Ming. That earlier extensive warfare had strongly motivated the development of a wide range of gunpowder weapons, and relative peace c. 1450-1550 ceased to motivate development.
The return to extensive warfare - and warfare that threatened the Ming state - in the mid-16th century restored the motivation, and Chinese development resumed. No longer the world-leaders, much of that development was adopting and adapting Western weapons, but that is still development.
There was also a relatively peaceful period during the Qing, from the end of the Qianlong Emperor's "Ten Great Campaigns" (finishing in 1789) to the extremely damaging and protracted Taiping Rebellion and bruising wars with the West (Opium Wars etc.). Note that the end of this relatively peaceful period inspired the Qing to modernise their army.
Second, money. The Ming operated on an extremely low level of taxation. The founder of the Ming Dynasty, the Hongwu Emperor, had grown up as a starving peasant (most of his family starved to death in his youth) and a wandering beggar. This contributed to his decision to have very low tax rates (which varied from about 1.5% to 3.3%). This left the Ming in perpetual financial difficulties. Officials were paid very little, necessitating either the acceptance of a life of financial hardship or padding their salary through corruption - the resulting entrenched corruption did not help the Ming government. The army was also paid very little - with a paper strength of almost 2 million men, it was simply not possible to pay them well. As a result, when they didn't simply desert, soldiers neglected their training and other duties to earn money to live on. None of this encouraged the spending of money on weapons development that was not seen as urgently needed (compare British defence spending of below 3% of GDP from 1923-1936, with accompanying reduced military R&D). For the difficulties the Ming had coping with the expenses of the increased military demands from the mid-16th century, see Ray Huang, "Military Expenditures in Sixteenth Century Ming China", Oriens Extremus, Vol. 17, No. 1/2 (1970), pp. 39-62. The Qing had similar problems, since they inherited the low taxation of the Ming. Their crises from the mid-19th century, while motivating modernisation, worsened their financial state, not only due to costs of deploying armies but also the loss of tax revenue due to the Taiping Rebellion and indemnities after their wars with the West and Japan.
The combination of financial difficulty and relative peace left the Ming and the Qing unwilling to develop new weapons and modernise during the periods of peace above. These were the times when Western development went ahead of Chinese stasis.
Other reasons are often offered. For example, the Chinese didn't fight equals, so most of their wars were not threatening to the survival of the state. The renewed warfare at the end of the above periods of relative peace was, for both the Ming and the Qing, very threatening to their survival. This doesn't work as a general argument, but is, for those peaceful periods, essentially what I wrote above. Another reason is "conservatism". Given the Chinese adoption of new weapons - whether imported ideas, imported weapons, or local developments - when needed and money was available, "conservatism" can't explain it. A third explanation is that gunpowder weapons aren't useful for fighting steppe armies. Since the Chinese found them very useful for fighting steppe armies, this doesn't work as an explanation, either.
For cannon technologies, we can also note that the Chinese had little use for wall-breaking cannons (since the walls of Chinese fortifications were often a solidly cannon-proof 10-20 metres thick). What they saw as the advantages of the cannons that became their folangji and hongyipao were improved anti-shipping performance (for larger guns) and more range and power in a lighter package (compared to older cannons), for mobile warfare and anti-personnel use from fortifications and ships. In some cases, the Chinese versions were much smaller than the European ones (if purely for anti-personnel use), e.g., breech-loading swivel guns for use on camel-back (e.g., in the Dzungar-Qing wars).
The flintlock is more mysterious, since the Chinese knew of the flintlock (as Needham notes, and also discusses the mystery, in Science and Civilisation in China V5-7). The flintlock does free the gunner from the match, with resulting improvements in safety.
The musket-with-bayonet could have allowed them to convert from a pike-and-musket army to an army similar to an 18th-century European army. The Qing retention of the lance as a major cavalry weapon (and the bow, which allowed the cavalryman to stay well out of bayonet range while threatening infantry) might have contributed. In addition, the very sorry state of training, discipline, and morale for large parts of the Chinese army at times might have made the spear/pike more attractive.
Modern artillery was too expensive for the Qing to be able to afford enough, and required precision manufacturing rather than traditional Chinese methods. Thus, the Qing were using a mix of muzzle-loading cannons, matchlocks, traditional cavalry, etc. alongside their modernised forces during the second half of the 19th century. Not enough time or money! (Perhaps if there was plenty of money, there would have been enough time.)
I am not an expert on this topic, but I happen to have read a paper on the topic few days ago. This might give you one explanation, I am sure the is more to it, but might be a start.
One of the core thesis of the paper is basically following: Weapon-technology in those days improved mostly by trial and error. When there was a successful new strategy, other people might adapt it or tried to counter it, which advances the technology as well. So number one there needs to wars and the best case a lot of it for some progress. This isn't really a big deal, because wars were common everywhere, but for example in Japan after the unified the country and internal conflicts were rare, technology kind of stagnated. Invading other countries was just not worth it at the time.
But it also depended on the enemy: The enemy the Chinese, the Russians and the Ottomans as well fought, were often nomadic step tribes. Gunpowder technology is just not very effective against them in that stage. There are no cities you can besiege and walls to blow up, just mounted riders with bows. And while firearms can be useful defending as well, the best way to fight this guys was with bows and cavalry, guns were just not that important and less need to develop the technology.
So that leaves Europe and India. Both of them were splitt into many states and fought wars constantly. The big difference was, that the Europeans just spent way more money on their wars. The states of that time were heavily focused on war and channeled a lot of resources into it. The Indian warlords on the contrary didn't have the same possibilities.
I hope that answers some of your question. You might look into the paper, the conclusion does put it nicer then I can.