The goals of the United States were to prevent North Korea from taking South Korea and unifying the two countries under a communist regime. This was part of the larger American policy of containment: to stop the spread of communism into areas where it did not exist.
The war began when troops from North Korea crossed over the 38th Parallel (which divided the two countries.) A secondary plan was to force American troops in South Korea to withdraw. President Harry Truman saw this as an attempt by the Soviet Union to spread its own influence as well as expand the communist bloc. In a speech to Congress, he stated:
The attack upon Korea makes it plain beyond all doubt that communism has passed beyond the use of subversion to conquer independent nations and will now use armed invasion and war.
The Korean War was actually a United Nations "police action," although the United States was a major participant. The plan was to force the North Korean troops back across the 38th Parallel and maintain the stability of the area. The war ended when President Dwight Eisenhower threatened to use nuclear weapons, and the Soviets (as well as the Chinese, who were now involved) backed down.
Although it is difficult to recreate the mindset in 1950, at that time the United States feared the Soviet Union would start World War III in a bid for global domination. The U.S. had just five years ago emerged from a very costly and destructive world war started by a regime with global domination on it mind, so naturally was still jumpy. Furthermore, in hindsight, people realized that if Hitler had been contained early, a world war might have been avoided. The U.S. was not going to make the mistake again of waiting too long to intervene.
So a chief goal of the U.S. was to avoid World War III by pushing back hard against what it understood as Soviet aggression. It wanted to discourage the USSR from expanding its empire—even if this meant going into a country quite remote from the U.S.
A more direct goal of the Korean war (the war was officially called a "police action") was to protect Japan, which the U.S. considered its linchpin to stability in Asia. Korea was too close to Japan for comfort, and the U.S. feared that a Korea unified as a satellite state of the Soviets could too easily try to invade Japan.
More specifically, the U.S. was determined to push the North Koreans back above the 38th parallel, the dividing line between North and South Korea, and keep them there. The goal was to nip communist expansion in the bud before it could grow into a monster that could only be contained by a major war.