Logically, your biggest and most powerful “ally” (Germany) surrendered. Why wait for the US to drop two atomic bombs? Was it the belief in the emperor’s divine crusade or all out fear of Americans invading the homeland?
Briefly: the Allied requirement was that the Japanese submit to "unconditional surrender," which the Japanese interpreted as requiring them to totally submit to Allied rule, in a way that would put the the Imperial House in grave danger. They worried that the Emperor and the high command would all be subjected to war crimes tribunals, with consequences being imprisonment and even execution. They worried that this would mean the total destruction of the kokutai, the Japanese imperial system, which they considered fundamental to their notion of what it meant to be "Japan."
So you can see why there was no rush for them to surrender, even though they were well aware the war was not going in their favor — it could have meant their lives, and potentially the destruction of what they consider to be their country.
They did not know about the atomic bombs. Those were deliberately a secret, and they were given no forewarning about them. They also did not know that the USSR, who had been neutral to them, would soon declare war on them; they hoped very much that it would not, because their military assessment was that they could not fight off the US and the USSR simultaneously.
The two strategies pursued by different factions in the Japanese cabinet can be briefly summarized as "war" and "peace." The "war" faction, dominated by the militarists who dominated the cabinet, wanted to mount a fierce resistance to the USA's attacks and invasion, with either the (not entirely ridiculous, but misinformed) hope that this would "bleed out" the US and they would choose to abandon the war rather than suffer further casualties, or the (entirely ridiculous) hope that somehow the Japanese would "rebound" in some big way and regain lost territory.
The "peace" faction, which included the Emperor and many of the civilian members of the cabinet (Foreign Ministry, etc.), instead was looking for ways to get the "unconditional surrender" requirement lessened, and maybe negotiate a diplomatic end of the war. They tried to engage the still-neutral USSR as an intermediary, with the hope that the USSR might be able to convince the USA that this was acceptable. They made some very tentative steps in this direction during the spring and summer of 1945, but failed to make any headway. This was because Stalin was dedicated to declaring war on Japan and seizing territory that was promised to him at the Yalta conference. The US was aware of these efforts by the "peace" faction because it had cracked Japanese diplomatic codes some years earlier.
This was the state of things in early August 1945, when the US dropped the atomic bombs, and the USSR declared war and invaded Manchuria. You can see how this would up-end the "war" and "peace" plans alike: the bomb further opened up the possibility of mass destruction without much loss of life by the Allies; the Soviet Union could not be "bled out" like the US could because it was not a democracy and Stalin didn't care; and the USSR of course would not be acting as a neutral intermediary in this situation. Assured ruin loomed before them, to a degree that even the militarists could perceive.
But even all of this only convinced the Japanese high command to offer up a conditional surrender — they offered to surrender except in as much as it impinged on the status of the imperial house. This was rejected by the US, who resumed conventional bombing. Finally, four days later, and after an attempted coup, the Emperor decided that unconditional surrender was acceptable, even if it meant his head, and announced it on the radio.
In the end, the US Occupation allowed the Emperor to retain his political status so long as he renounced any claims to being a god, but this came some time later.
Whether the atomic bombs or the Soviet invasion were the major factors in the Japanese decision to offer up a conditional surrender is something historians still debate. Most think it was some combination of both.
For a longer and more detailed description of the above, see Hasegawa's Racing the Enemy.