OP asks a question that seems simple but individuals and societies have been grappling with for years. I've been personally grappling with this as well for most of my adult life, and it has definitely been at the forefront of my mind since I've been raising kids as I want to arm them with the most moral principles that I can so the complications of life won't cause them to be suicidal and depressed. Here's what I've come up with:
All societies are tacitly based on consent and trade-offs. Even the most totalitarian government of antiquity could be overthrown by a large enough mob (losing consent) though they would only throw them off if the regime in power failed to attend to the needs of the governed for long enough (the trade-off). It's really only in the last 100 years that technology has really been able to isolate the elite from the rabble in such a way that regimes like N.Korea and Xi's China can exist.
The founding fathers of America added something else to this equation: The idea that Man as an individual had inherent rights regardless of government (endowed by their creator) so any government that had intent to rule the people had to serve this other master, as encapsulated by the preamble of the Declaration of Independence: "...We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.–That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed..."
No longer would it be implicit that the .gov had ultimate authority, in this mode the people have ultimate authority but 'loan' that authority to the elected .gov in order that things that are difficult to impossible for the individual to effectuate have the power of the collective. Every thing the US government should be doing is encapsulated by this: "...in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity..." -from the preamble of the Constitution.
One of the issues with this that has been brought up in this and other threads is all of this relies on a single precept: The "people" need to have some agreement on what is "moral", which used to be defined by Christian values as understood by everyone then and now if you are being intellectually honest. Regardless of what you think about religion in general and Christianity in particular (or Judeo-Christianity, there's certainly precedent for lumping them together and IMO the values are the same, Christianity just reinforces the morals of the Torah). At John Adams said: "Our constitution was made only for a moral and religious people. It is wholly inadequate to the government of any other".
There's a line from the Book of Revelations that illustrates the problem with being ambiguous with where you stand regarding morality. "I know your deeds, that you are neither cold nor hot. I wish you were either one or the other! So, because you are lukewarm--neither hot nor cold--I am about to spit you out of my mouth." This has been interpreted as being ambivalent towards religion or God, but it has also been interpreted as the danger of being a "moral relativist". This is also what Abraham Lincoln meant when he stated in the "house divided" speech:
"A house divided against itself cannot stand. I believe this government cannot endure, permanently half slave and half free. I do not expect the Union to be dissolved - I do not expect the house to fall - but I do expect it will cease to be divided. It will become all one thing, or all the other."
This as all a preface to the answer to OP: Doing immoral things as a collective is wrong. A government shouldn't be so powerful that 'the collective' is capable of signing off on "immoral things". For a government to get that powerful is a sign it is too powerful.
However, if you are a Quaker you will say that doing anything that may kill another being is wrong. If you are a pacifist you feel the same. If there was a majority of Quakers or pacifists then we would have a government that would reflect that. Good luck protecting your borders.
If you are Hindu you think that it's immoral for the individual to kill cattle so the government reflects that. Hope you don't care for cheeseburgers.
Also, one of the reasons why the head of the executive is supposedly "commander in chief" as well as technically being the head of the enforcement arm of the federal government is that person, that single person, can bear the burden of making the decision, which is what the executive is supposed to do, moral or not. If you are truly a moral person who shares those morals with those who elected you then the weight of "immoral things" will be such they should only occur under duress and in the most limited way possible. If they were truly immoral and without justification that person can be removed. I think the problem is definitional which is why I prefaced it with so much background. One persons "murder" is another persons "submission hold that went awry".
So, we will adopt a set of morals collectively and have a government that reflects those morals; we have to agree on some basic principles or we will be "lukewarm" or "half and half". We will find ourselves in a hot civil war where neither side will really have the moral high ground if they do what they have to in order to win it, but if you truly believe your moral superior you will have to win it or the reprehensible side will win out. What comes out the other side will cease to become ambiguous and this conversation will enter the rhetorical realm again.