You seem to be assuming the government wants people to break its laws, so it gets to punish people.
It costs the government money to put people in prison; not only the cost of their food, shelter, supervision, etc. but also the cost of removing a taxpayer from the workforce. Furthermore, a democratically elected government that is increasingly imprisoning more and more of its people is not likely to remain popular. It is in the government's best interest to only enforce laws that the majority of people are happy to obey.
Civil disobedience is a technique that protesters who think a law is unjust use to take advantage of this interest. By deliberately disobeying the unjust law and accepting the punishment for it, they help put pressure on legislators to change it. The government prefers a situation in which no laws are ever broken--that's why it makes such an extensive effort to incentivize citizens not to break them--but in the case where enough people feel it is in their best interest to break the law even with the punishment that comes with doing so, as people who use civil disobedience do, then the law must change if it is to reflect the will of the people.
The example you gave re: homosexuals/death penalty is extreme, but follows the same principles, as long as that government is democratically elected. Most democracies don't get to the point where they're throwing the death penalty around willy-nilly though, thank God.