This is a very complicated question, but there are several reasons that Turkey not only didn't join the Axis but remained neutral until the last days of the war.
First, the Nazi-Soviet Pact of 1939 forced Turkey into alliance with Britain and France, remembering that the Ottoman Empire had fought against Tsarist Russia in World War I and had difficult relations with Russia for three hundred years prior to that. Both Germany and the Soviet Union posed potential threats to Turkish security, with the Soviets long interested in control of the Dardanelles and Bosphorus.
Axis successes, however, and Hitler's desire to guard his army's southern flank when he eventually decided to invade Russia, resulted in a German non-aggression pact with Turkey signed 18 June 1941, just four days before the invasion. In this regard, Turkey was playing "both ends against the middle" with an alliance with the Allies and a non-aggression pact with Germany.
Churchill and Roosevelt pressured the Inonu Government to allow an invasion of the Balkans and the placing of British bases in Turkey, which the Turks noted would violate their pact with Germany. They also informed the Western Allies that Turkey was not militarily strong enough to enter the conflict on her own.
However, the Gaylani coup in Iraq earlier in 1941 resulted in increased German pressure to allow their troops to pass through Turkey to fight in the Middle East. This too was rejected, with the end result being the non-aggression treaty which allowed Turkey to sit on the sidelines while Hitler invaded the Soviet Union.
That said, Turkey declared war on Germany in February 1945, primarily in order to have a spot at the Conference on International Organization, which eventually resulted in the founding of the United Nations.