If a solider were ordered to go to war and they thought the war was very bad for the country, could they just resign or would they still be punished for not following the order?
Usually soldiers are not allowed to resign at will.
If they refuse to follow orders, they can be charged with a variety of charges, from insubordination to mutiny.
If they leave the army on their own, the term is desertion, which is illegal too.
A difference is drawn sometimes with civilians that are called to serve but refuse too; since those were not soldiers at any moment the term draft evade is used (illegal too).
Members of the military must obey orders, unless those orders are illegal.
Soldiers and civillians who are drafted may raise "Conscientious objections", which is "firm, fixed, and sincere objection to participation in war in any form" (you can't object to one war but not to another). Soldiers who apply for conscientious objector status are reassigned to non-combatant roles in hospital, chaplaincy or logistics, etc (which can involve being in a combat zone, but not actively engaging with the enemy). It is only granted after an investigation by a senior officer. So you can't raise conscientious objections on the eve of being sent to battle.
I'm gonna expand my somewhat snarky comment into an answer, mostly because I think the military is a special case, in ways that are not apparent to civilians.
If you think about it, most of the time army personnel have a bit of a sinecure job. The pay's OK, they have good job security and they get to do somewhat "fun" stuff (if you like military). Many accrue rights to pursue tax-payer funded education afterwards as well.
However, come wartime, that cozy gig is flipped right on its head. They have to follow orders that puts them in direct harm's way and very likely could get them killed. And they have to do so unquestioningly (assuming it's a legal order). Their closest buddies may get killed and they still have to stick it out.
If war is hell, then they have the job from hell. Literally.
If they could just "do like civilians", they could, yes, resign. And whatever country is employing them as soldiers would then lose its war.
So the way it works instead is that you enlist for a number of years, which is basically a commitment that you will do your job for that duration. You don't get to leave until your enlistment is over. Leaving would be considered either desertion or AWOL. Typically, soldiers will repeatedly re-enlist until they want to leave.
From Join the military (USA, but applies elsewhere too):
Enlisted members make up most of the military workforce. They receive training in a job specialty and do most of the hands-on work. Usually, you’ll sign up for four years of active duty and four years inactive. After you’ve completed your active duty time, you can either extend your contract or re-enlist if you want to continue serving.
Note that during the nasty years of the Iraq and Afghanistan wars, around 2004-2008, there was a lot of pressure to keep US servicemen in by re-enlisting them. That and the controversial stop-loss program.
I will finish up with a quote I still fondly remember from living in France in 1990 and which shows exactly why resigning rights aren't a great idea for armed forces. In a TV interview, some lady made the following statement:
Mon fils ne s'est pas engage dans l'armee pour aller en guerre.
My son didn't join the army to go to war
One wonders what French taxpayers thought they were paying him for instead.