There were a lot of factors, the most important were the geographical location, the political alignment of forces, the peculiarities of the Japanese mentality, and even simple luck. But you need to talk about them in order.
Remember the historical spread of European imperialism. At first, a relatively close and defenseless America was conquered. Then it was time to establish trade outposts in Asian countries, which only much later grew over the perimeter of completely conquered or politically dependent lands. It was only in the 19th century that the time came for Africa, which, although it was nearby, at first was not of special economic value.
In this regard, an extremely distant country with a dubious strategic position, but very densely populated and militant, was not a primary goal and interested Western sailors only as a place for trade. In addition, there are absolutely no natural resources, which many Japanese are proud of, who managed, despite this, to achieve economic prosperity. In a sense, in the colonial era, this became their salvation.
For the same reason, the great distance from Europe gave Japan a lot of valuable time and brought some useful lessons. The political leaders of the land of the rising sun saw a ring of Western possessions shrink around their power, and in this situation they reacted very correctly.
The Portuguese first landed on the Japanese archipelago in 1543. Despite the fact that they arrived here for the purpose of trade, to get the best commercial conditions and to curb those Japanese feudal lords who would be too greedy, they showed their muskets. But the local population was very different from the American Indians, who believed that guns fired with lightning stolen from the gods.
Several sailors were bribed, which allowed the islanders to quickly copy European firearms, which in just a few decades spread throughout Japan.
In 1596, the Spanish galleon from Manila was thrown to the Japanese coast during a storm. The crew was detained, but people were treated well. The local prince invited officers for lunch. As soon as the sake began to flow, the Spaniards began to brag about how they conquered countries, sending religious missionaries, followed by soldiers and officials. A detailed account of this conversation came to the then shogun, who immediately banned Christianity on his land.
In addition, the Japanese princes, although competing with each other, immediately united when it came to an external threat. For example, in India, Muslim Rajs helped the British against the Hindus (and vice versa), making a significant contribution to the capture of their homeland by Europeans.
But this was not the case in Japan – because of the local island psychology, there were no traitors there. When the Portuguese decided to show their teeth and teach a lesson to arrogant Aborigines, the Japanese guns fired back. When the Spaniards sent the Jesuits here, the government began to persecute Christians. When the Americans introduced the oil embargo, Japanese planes flew up over Pearl Harbor. In the future, the United States had to use nuclear weapons in order not to storm one island after another and not to meet stubborn resistance and insane heroism.
Was in maintaining Japanese independence and a considerable element of good luck. First, the Spanish-Portuguese colonial rivalry helped her, then the competition between Holland and England, and then the confrontation between the Russian and British empires. And when in 1854 the country opened up to the outside world, in just a few decades, it pulled forward so much that it itself became a predator from herbivores.