There are three reasons the Soviets pulled their missiles out of Cuba.
In response to the Cuban missile threat, the U.S. had moved a massive and overwhelming amount of military forces into Florida. If the Cold War had turned hot Cuba would have lost, in any kind of conventional shooting conflict. Communist Cuba was still in an embryonic state, and would never have been able to withstand the full might of the U.S. military muscle, and Russia knew this. The Russian missiles in Cuba would have all been destroyed in the first few days of the conflict, and then most likely we would have invaded and occupied the country to permanently fix the problem. The Marine Corps units to do this were all set to go and sitting in Florida ready to seize the Cuban beachheads. In the end, the Russians would have lost Cuba as a puppet socialist communist-friendly country. It was better to withdraw the missiles and save an ally, then have the entire ally be eliminated completely.
The second reason was logistics and the lack of a Russian blue-water Navy. He who controls the sea around an island, controls the island. The Russians did have a Navy, and a pretty good one, but with the exception of their submarines, it was mostly a coastal (brown-water) navy, and not one that could project power far from the homeland. The U.S. Navy was only 90 miles from it's bases, with all sorts of support, air cover, and reinforcements available, while the Russian fleet was over 5500 miles from home base, fuel, air cover and any resupply. The U.S. Navy had also redoubled their efforts to find all the Russian submarines currently in the Atlantic and around Cuba, and had done quite well in forcing several to the surface, so the Russians knew if the shooting started most of their subs would have been immediately destroyed too.
The third and last reason was neither side was eager to start World War III. Cuba wasn't really worth it, but each side had to call each other's bluff. John F Kennedy made it known to the Russians that if they didn't take their nuclear missiles out of Cuba, that we would put our own nuclear missiles in Germany and Turkey, right on top of their borders as well. And the Russians didn't like that idea at all.