Ted Sorensen was one of Kennedy's closest advisers, and he wrote in his JFK biography that after Gagarin's flight, the President was presented with several options including a two-man spacecraft, dual spacecraft rendezvous, orbiting laboratory or waystation, EVA, and moon landing (there may have been others, but the book, Kennedy, isn't available online, so I'll have to check tonight).
The President's advisers, which included Jim Webb (NASA Administrator) said that the Soviet Union would beat the US at every one of these goals, except perhaps a lunar landing. It was far enough away developmentally to give the United States time to catch up, but big enough to claim as victory.
JFK's exhortation for America to go to the Moon was far from simple rhetoric. It was still a significant challenge, but it was feasible to the (President's) experts at the time, which is why Kennedy chose it.