The first and most important thing to understand is the Soviet influence in the world ar that time. Being the first communist nation, nearly every communist movement in every nation looked to Stalin for guidance. This is very evident in the eastern bloc nations during the Cold War, where every political and armed communist movement was funded, armed, and trained by the Soviets. Every communist leader and nation looked to Stalin as the father of communism, and had almost complete and unwavering loyalty to him and his Stalinist version of communism. (Except for Yugoslavia, which is a special case.) So when the communist movements in Asia began to pick up, they naturally looked to Stalin for guidance and help, and acted almost as satrapies or vassals, asking for permission and advice on every decision. Now, Mao had just mopped up a bloody civil war and had founded the new communist government in China. He expected to bask in Stalin's praise, but was almost completely ignored by Stalin, who was busy focusing on the more pressing matters with the West. Kim Il Sung had been asking Stalin for permission to invade South Korea for awhile, and in January of 1950, Stalin finally gave permission. Naturally, Mao wanted in on the action and was hurt when he was not offered a substantial role in the planned invasion. Playing politics, he feigned not being able to mobilize his troops due to "insufficient armament". He eventually stopped the bluff and agreed to help on his own terms. Mao began preparations for the invasion in August of 1950, more than a month before the Inchon landing and the UN counterattack. (This rules out MacArthur's role in Chinese intervention, a common misconception.) Mao wanted to drive the UN forces past the 38th parallel and out of the Korean peninsula, securing a decisive communist victory. Mao won victory after victory with his seasoned "volunteer army" , hardened from its experience in China's civil war. Mao turned down many generous peace-treaties offered by the UN, one of which included a removal of all UN troops from the peninsula. Mao was in a strong position on the battlefield and at home, while his popularity grew in China and the communist realm. Mao's goals in Korea had been achieved, although the borders would settle along the 38th parallel, where they had been at the start of the war. He wanted to gain fame as a communist hero, establish China as a dominant communist nation, and to strengthen his position in the good graces of his oppressed people. And, to his personal gratification, he did this against the advice of the great Stalin.
Follow up question: What's the consensus on whether UN forces actually did cross the Yalu river? I have a friend from China who insists that they did, and while I'm inclined to disbelieve this as Chinese propaganda, I am unfamiliar enough with the subject that I don't want to dismiss his claim out of hand.
I once heard from someone that part of China's consideration of joining the war has to do with the fact that Soviets were still occupying part of Xinjiang, which they withdrew from after the war was over. Could anyone show me if there is at least partial truth to this claim?