Arguably this is a staple of the idealized samurai we imagine even in the modern age. From Edo period stories such as the 47 ronin, to the tales of Minamoto no Yoshitsune or all the way back to Yamato Takeshi and seemingly endless stories surrounding the Sengoku Period, the question I want to ask is vague and spans a wide period of time and yet seems to be applicable even until very recently, relatively speaking: Why?
It's hard to grasp as someone who very much prefers not to die (and reading these stories, these people certainly didn't seem keen on dying either, well most of them) why someone would kill themselves (or their family) upon a defeat or extremely embarrassing situation. Often you also see stories about those going into battle with the full intent on dying (or indeed being commanded to, in the story of Kusonoki Masashige). What exactly were the main influences of this culture?
If I were to guess from my readings, the buddhist idea of reincarnation played a part. The idea that death was not the complete end. But at the same time, samurai or warriors were (with some exceptions when they "retire") not exactly known for their buddhist qualities, heck even the monks seemed to barely be. Another factor may be that they'd be killed anyways which offers a convincing reason as to why, but I can't help but think that just running away or escaping to the countryside or the mountains wouldn't also be an effective method depending on the situation. However I feel like there has to be more to it than that, would someone please enlighten me?
Perhaps an auxiliary question: How idealized is this view of samurai? Certainly there are many primary sources of these kind of stories, however that simply points to the idea that these acts were idealized even during that time period. In reality (as best we can tell), how often was it that one would really stick by these ideals? Rather than them just being just that, ideals.
Edit: A passage just came up from the story of Kou no Moronao , translated in the book Legends of the Samurai by Sato Hiroaki that I feel is relevant, brackets are my doing:
" Rokurou [a retainer of the Takasada] was pleased how easily he had accomplished his mission [to deliver a child of Ennya Takasada to safety] . . . He said to the other men 'as long as I have any arrows left, I'll defend all of you. Go inside and stab the lady [Takasada's wife] and the child to death, set fire to the house, and disembowel yourselves'. "
After they were killed:
"The remaining twenty-two men were all relieved to see this. They untied their topknots to let their hair loose, bared the upper parts of their bodies and whenever an enemy approached, dashed forward to fight with swords. They knew there was no way they could save their lives. They also knew that to continue killing in the circumstances would merely be adding to their sinfulness . . . They set fire to the entrance of the house, rain into it as the fire grew fierce, and each disembowelinh himself in his own way, burned to death."
The premise of the question is incorrect.
Before the Edo period, samurai as a whole were famously disloyal, which could be a reason the stories play up loyalty of a handful of "good" characters. During the Edo period there were no wars and no domain were allowed to hire samurai who were kicked out of other domains. So there were no real way to test the loyalty of the samurai in general.
Seppuku most often happened when death was seemed as certain. Getting captured after a defeat in battle more often meant execution for the leadership of the defeated. In this Japan wasn't really all that unique. Many Romans also famously fell on their own swords. Seppuku was also used often as punishment. The person's refusal to cut his own belly would just lead to his head being cut off like a criminal. In this case Seppuku was a priviledge.