The American policy of containing communism led to the Korean War and later to the Vietnam War because it committed the United States to resisting communist expansion. These wars were fought for that purpose.
The containment policy said that the United States would try to prevent communism from expanding. The US would not directly try to overthrow existing communist regimes like that of the Soviet Union. However, it would try to prevent any country that was not already communist from becoming communist.
This led the US to become involved in the Korean War. North Korea invaded South Korea with the intention of reuniting the country under communist rule. That would have spread communism into a country (South Korea) where it did not already exist. In addition, it might have threatened such countries as Japan. Therefore, the US entered the Korean War to prevent communist expansion.
The same thing happened in Vietnam. The US entered that war to prevent South Vietnam from being controlled by the communists. This turned into the country’s longest war because the communists were not easily defeated. The US felt that it had to keep trying to contain communism and so it kept on fighting, making this the longest war in US history.
The United States had traditionally been isolationist with a focus on the Western hemisphere as its sphere of influence, but that officially changed after World War II. This was in part due to the 1947 Truman Doctrine. The U.S. was concerned that the Soviet Union was supporting communist groups in other countries to try to spread global communism. The U.S. was also concerned when the British refused to get involved in what the United States thought was a Soviet effort to back communists in Greece. The Truman Doctrine committed the U.S. to actively containing or stopping the spread of communist governments.
Both the Korean and Vietnam wars were a continuation of the containment policy. In 1950, communist North Korea, which was supported by the USSR, tried to expand below the 38th parallel and take over South Korea. Even though Korea was so far away, the U.S. felt compelled to intervene to stem the spread of communism. Likewise in Vietnam, the Soviet-backed Viet Cong fought to reunify the country under a communist government. The U.S. got caught in long, costly, and unpopular war to try to prevent that from happening. The Viet Cong used guerilla warfare, which was difficult for the more traditional U.S. army to combat (it was equipped to meet another army in battle), but the U.S. stayed in the war for a very long time because of its fear of the Soviets gaining power in Asia.