People started trying to figure out how to do it long before WWII. The ancient Greeks figured out that the moon was a distinct object that orbited the Earth, and a Syrian-Greek poet living in the Roman Empire 2000 years ago wrote a sci fi story about a ship traveling to the moon via a powerful water jet.
The Nazis were working on it and had made great strides in the rocket technology needed to do so. After the war, a lot of Nazi rocket scientists defected to the West or the USSR (likely in exchange for their freedom and/or lives).
Both the US and the Soviets prioritized rocket research alongside nuclear weapons, and a space race was a great way to boost public support for said research; most people get more excited about adventure and exploration than they do about the prospect of blowing up the planet they live on.
If you can make a rocket that can fly with such precision that you can land a person on the moon and then fly them back to Earth unharmed, then you can also make one precise enough to deliver a nuclear warhead to any specific location you want on Earth, so both governments stand to gain a huge strategic advantage from this research.
Latest Answers