How did Switzerland became Switzerland?

Switzerland was created in 1291. At that time the modern concept of independent countries did not exist.
The original Swiss confederation was much smaller than it is today. At that time the four original cantons were constituent parts of the German empire, which agreed to a pact of mutual defence to defend what would today be called "States rights" against attempts by the feudal rulers of larger German states to absorb them.
They became de facto independent over the next years, though they were not recognised as legally independent by the German empire until 1648, after many other small German states had joined the Confederation.
Apart from during the Napoleonic wars, Switzerland remained a technically defence treaty of many independent countries (sort of medieval NATO) until 1848 when they finally decided to have a permanent federal government.