There are at least two ways to look at this question.
First, why was there a war at all? The main reason for this was the communists’ desire to expand and the West’s strategy of containing communism. After WWII, Korea was split into communist North Korea and non-communist South Korea. The communists in the North, backed to some degree by China and the Soviet Union, invaded South Korea. This was in keeping with the broader communist goal of expanding the reach of their system. They had taken China in 1949 and wished to keep expanding. Meanwhile, the US (as shown in the source) and its allies wanted to stop communism from spreading. This is shown by the fact that the UN sanctioned the military action and many other countries sent troops.
Second, why did the war last all the way to 1953? The main reason for this was overreach on the part of the US, leading to Chinese participation in the war. After North Korea’s initial success in the war, the US pushed back and routed the North Koreans. The US invaded North Korea. US leaders did not heed signals that China would enter the war if the US got too close to the Chinese border. When the US got too close, China entered the war. The size of the Chinese army helped drive the US back, leading to a stalemate that lasted until 1953 when peace was made.