I can only think of one successful example of countries uniting after a conflict with the explicit goal of never letting anything like that happen again, and without it being explicitly about defending against a greater external threat: the European Union.
The idea of the European Economic Community (what would evolve into the European Union) was that those rival powers should develop mutual dependence, especially economic interdependence. This means they would be less prone to fight because of it, would be more used to work together, and would all be more prosperous out of it, decreasing scarcity as a cause of war.
This would later lead to political integration (to an extent), with a common Parliament and a Supreme Court-like instance of justice and a common police organism to help cooperation between national ones. Military integration is also a recurring idea. The main point is, there is a common European polity, as flawed and politically weak as it is, and risk of conflict between members is now gone.
So this would work as a template for the world union, with varied parameters to be tweaked as desired - a trade union, a monetary union, a top-heavy federation or a loose confederation, a strong or weak supreme judiciary instance that has authority to solve conflicts between nations, a paragon of democracy or a terror police state...
It is, however, difficult to accomplish, and several factors played, and will help set this world on its way:
A common identity: Even though national and often regional cultures had strong identities, there was still a sort of European identity, an idea that despite big differences, there was a common base of fundamental values and shared history. A French general described the impending WWI as a madness of an European civil war. It is easier to have some common identity when there are others outside of it to help define it, but it's probably not impossible for the entire world to have it to an extent.
A long-standing idea: While the ECC itself was pretty new, the idea of an unified Europe was an old idea. On the mythical side, the Romans and Charlemagne had apparently done it. Napoleon had done it with the Continental System. Following his fall, the Concert of Europe was a rather similar idea - though based on political negotiations and balance of power instead of economic integration, it proved itself fragile and its failure mode apocalyptic. Even Hitler's dream was a twisted perversion of the same ideal, a united Europe. So the UE is the last of a series of attempts, and the first one to actually work.
Something so big and soul-crushingly horrifying that "never again" was acted upon: Ultimately, it wasn't the war itself that shocked nations into unifying. It wasn't the millions of deaths, nor the bombed cities, nor the starving populations, nor the people burned alive, nor even the weapons of mass destruction. All this had happened twenty years before, and only made things worse. What really made the difference, ultimately, was the Final Solution.
The pre-WWII Europeans thought they had, as a civilization, risen above the barbarism of the previous centuries (or some neighbors - racism was a thing). And then, the nation with the greatest scientists, artists and philosophers in the world had just industrialized genocide. This was literally unimaginable, and even accepting that it had really happened was difficult. I'm not sure any other civilization slapped itself in the face so hard, short of actual destruction. To this day, this very specific "never again" is used to justify the existence of the European Union, even though it is almost gone from living memory.
The problem is, how do we shock those three factions to their very core like that without the "millions of deaths and destroyed cities" part of WWII?
Keeping the same "Boy we are now civilized people, not like those blood-thirsty clods from the Dark Ages, eh?" mindset, we can use a similar, ah, solution.
The three factions are engaged in a bitter cold war. They all have nukes, so it doesn't go full WWII, but the local equivalents of Stalinism and McCarthyism appear alongside costly, ineffective proxy wars witnessing more and more war atrocities. At some point, "unsafe" people are isolated in camps. For their own good, of course. To be reeducated. Then to work (to death) for the State they (would inevitably have) betrayed. Then, because there are now so many "unsafe" people, to be "processed".
At the same time, all those proxy wars, defensive armies and police state apparatus are a strain on the economy. Clothes, heat and food becomes scarce.
But those three factions don't go full 1984. There are neutral nations that managed to make it off pretty well, some of those with their own, mostly free journals. Factions themselves aren't monolithic, and are ripe with internal bickering. The idea of democracy, or representative monarchy, or whatever political system was before creeping terror police state, is still there. Propaganda even keeps pretending that's the thing they are defending.
At some point, a faction elects a Gorbachev-like figure that decides that the Cold War lasted long enough, and it is time to end it. After all, we have nukes, it's not as if they would invade us anyway. The hardliners try to take power, but fail because they are inexperienced at it and the population prefers the end of the Cold War (and its promised better days to come) to a military dictatorship. The whole thing turns into political chaos, but it gives ideas to the other factions and the whole thing ends up unraveling in an anticlimactic (meaning not mass-destructive) political mess.
That's when the secret "processing facilities" are revealed to the world in all their extent.
And that's when the new generation of rulers that more or less emerged on top of the mess decide that a Cold War can never be allowed to happen again, and the best for that is to create an union.