They have that ability through the power vested in them by the citizens, to effectively project the force that the citizenry, as a whole, desires to project.
If the citizenry cared, they can do something about that. (They don't, for a lot of reasons. And they're wrong, but they don't.)
If the government acted for the citizenry as a whole, then they wouldn't have to ban these transactions. At best, the government imposes the majority view on the minority. I think that in general this is more dangerous than helpful. Imagine that the government, with full support of 80% of all US citizens, banned doing business with anyone of Iranian descent, not just living in Iran. That's not feasible in the current climate, but it's certainly possible. I think I'd prefer it if such laws were fundamentally unenforceable.
So if country A is at war with country B then the government of A shouldn't be allowed to prevent company C from selling items which materially benefit and provide aid and comfort to country B?
If the citizenry cared, they can do something about that. (They don't, for a lot of reasons. And they're wrong, but they don't.)