The Israel Defense Forces (IDF) came into being on May 31st, 1948, which is 17 days after Israel’s declaration of independence. But by that time Israel had already been defending itself against Arab attacks for six months since the Arabs started attacking the Jewish population in that area right after the UN adopted the partition plan, which was intended to form both a Jewish state and an Arab state in Palestine. The Arab Higher Committee rejected this plan, and attacks began, first by Arab Palestinian gangs, and later by the armies of four Arab countries – Egypt, Syria, Transjordan (Jordan), and Iraq.
Even though it was just formed, Israel already had a defensive force, mostly based on the Haganah (which means “defense” in Hebrew), a paramilitary organization that had existed since 1920. The Haganah was formed following Arabs riots in Palestine in 1920–1921, which mostly targeted Jews. The British, which controlled the region, did not provide adequate protection for the Jewish population, and essentially forced the Jews to fend for themselves. Initially, the Haganah was localized and poorly armed, and lacked a strong central command. But following severe Arab riots in Palestine in 1929, it was reorganized as an effective underground army, encompassing nearly all the youth and adults in the Jewish settlements, as well as thousands of members from the cities.
In 1941 the Palmach was formed, which was the elite fighting force of the Haganah. The aim of the Palmach was to defend the Palestinian Jewish community against two potential threats: the occupation of Palestine by the Axis in the event of their victory over the British in North Africa. And, if the British army were to retreat from Palestine, Jewish settlements might come under attack from the Arab population. Palmach members were highly trained, and by 1948 it had grown from the original 100 men to over 2,000 men and women in three fighting brigades and auxiliary aerial, naval, and intelligence units. When the IDF was officially formed, the Haganah and Palmach became its core.
In addition to the Haganah and Palmach, there were also the Irgun and Lehi. These were small, breakaway paramilitary organizations, which specifically targeted the British forces in Palestine when Britain reneged on its promises to facilitate the creation of a Jewish state, and forcefully prevented Jewish immigration to the area, including of Jews trying to flee the Holocaust. While there was antagonism between Irgun, Lehi, and the Haganah, these two organizations were also folded into the IDF during the following months.
To summarize, Israel was able to survive because it already had a military force by the time that it was formed. By the war of 1948, the Haganah had already been protecting Jews against Arab attacks for 28 years. And indeed, even that was preceded by Hashomer and Bar-Giora, smaller paramilitary forces that existed since 1907. In other words, 40 years of preparation, due to repeated Arab attacks. It is important to note that despite this lengthy preparation, initially, the IDF was inferior to the invading Arab armies in both numbers and armament. For example, the Arab armies boasted 270 tanks, 150 field guns, and 300 aircraft. The IDF had zero planes and three tanks. There’s just so much you can do as a paramilitary organization, while the British army is trying to suppress you.