It's basically determined by conventions of international law. Eg. Geneva Convention is one of these instruments, as are various UN declarations. Geoffrey Robertson, QC, has written a few books around these issues (specifically re war crimes). I have studied this as part of history studies but ages ago.
It's a vast area. But some interventions are justified, based on international law. Eg. the Invasion of Uganda to get rid of Idi AMin by surrounding countries, around 1979. Amin's regime had not only caused suffering for his people, but seriously destabilised the whole region. So leaders from around Uganda's borders, the countries there, got together and invaded to get rid of him. Nobody I think could blame them, this is near to a righteous invasion, they were interested in the interests of Sout-East African region as a whole.
By contrast, things like various invasions done by USA and former Soviet Union (USSR) are agreed by experts in international law/history field to be illegal. Eg. USA into Vietnam, or more recently Iraq. & USSR into various East European countries, esp. Hungary in 1956, Czechoslovakia in 1968, and Afghanistan in c.1979.
The Korean War is a more curly one, the defence of South Korea led by USA was under the aegis of the UN. The Communist north was trying to get the whole Korean peninsula for themselves, it was a political land grab, nothing more. Compared to that, Vietnam was different. The French colonialists left, after President De Gaulle's policies of decolonisation, and USA simply moved to fill the void, and they thought that Vietnamese Communism was a threat to the whole region, which it was not. All those scare tactics like "reds under the bed," all that Cold War rubbish.
So it's complex as to what invasion/intervention is right and what's illegal. In any case, history and hindsight often decides. But maintaining national sovereignty as much as possible is paramount.