How did Confucianism contribute to the stagnation of Chinese society when especially compared to the West?
If Confucianism was not responsible, why did China stagnate so much?
Although I argue that Confucianism is not a religion, I would say that this question is similar to ones along the lines of "Why is Christianity the source of our world's problems?" Confucianism itself was not really the cause of stagnation, but rather the problem stemmed from corruption, greed, and the general follies of man. To say that Confucianism naturally held back the Chinese from 'westernizing' is incorrect - the Jesuits saw so many similarities between Confucianism and Christianity that they regarded it as being one of the places where conversion could progress most smoothly.
Confucianism was the State philosophy of the Han and Song Dynasties, both of which saw incredible innovativeness in the fields of science and technology. In particular, the Song reorganized land distribution to triple food output (despite having half the land of the Tang, they were able to support a population twice the size) and were producing steel at a rate that was unmatched until the British of the early 19th century.
Leading up to the stagnation, China's weaknesses were caused largely due to attempts to keep China isolated, an increasingly reactionary and conservative attitude that flourished during the end of Qianlong's reign. This was in part due to rampant corruption under Heshen, Qianlong's favorite court official (and rumored lover), who siphoned off about 20% of the Empire's annual revenue into his private estate. The nation was highly destabilized by Heshen's nepotism and corruption froze the bureaucracy, and in an age where Europe was literally steaming ahead, this was a key period in which China was overtaken in technology and administrative innovativeness.
If you look at the beginnings of the "stagnation" period, you will see that many of the more progressive people were actually Confucian scholars. Men like Feng Guifan and Zeng Guofan were scholars and spearheaded the Self Strengthening Movement. Other leading scholars, like Kang Youwei, were behind the Hundred Day's Reform. What resulted in the failure of both of these were, again, corruption and political intrigues. The Self Strengthening Movement lasted for about 30 years and was an attempt to reconcile Confucian ideals with Western conceptions of modernity. It was highly successful in building up the Chinese military-industrial complex, and during China's first war with the modernized Japan (1894-5), the world expected China to win. China's defeat lay in the corruption that resulted in ammunition being filled with sawdust rather than gunpowder, ships missing essential parts, insufficient funds to pay soldiers, etc. In fact, during this time, some of China's warships rivaled those of Germany in size.
The Hundred Day's Reform failed because of Cixi's fear of losing power. The resulting power struggle represented the last time that Imperial China really had a chance to reform. By the time of the Republican Era, Confucianism as a state ideology had gone out the door, yet the corruption in the Guomindang not only lost them the Civil War to the Communists, but also drove the nation into its darkest days since the Mongolian Invasions.
Tl;dr Confucianism is not the cause, corruption and human weakness were responsible.
Confucian ideals place heavy emphasis on hierarchical roles in society. This led to interesting dynamics between politicians and their own priorities for their own decision making. First we need to look at Confucian relationships:
Each relationship has implied duties that one must abide by. This, unfortunately led to rampant corruption as people in power still abided by these duties. Politicians needed to look after their family and younger politicians needed to be dutiful to elder politicians. These power dynamics and corruption led to political instability. Hegemonic dynasties with smaller tributaries acted differently than Europe's more stable model of multipolar states. That being said it is hard to see the time period of Confucianism as stagnation without the lens of the western world.
There are other limiting factors that I would argue would lead to the stagnation you are referring to.