In general
Who pays for the damages caused by the government attempting to
enforce a preempted or unconstitutional law?
It depends upon the legal theory asserted and the parties involved.
Governmental liability for damages in the United States involves a quite complicated body of legal doctrines, statutes, and state and federal constitutional provisions, in part, because of its federal structure, and in part, because of the piecemeal way that this body of law has arisen historically.
Often no damages can be awarded even if the person harmed by the attempt to enforce the preempted or unconstitutional law prevails in court. But there are some circumstances when money damages or attorney fees incurred in the lawsuit by the person bringing the lawsuit can be awarded if the person bringing the lawsuit wins the case.
Specific rules in particular cases
A civil action under 42 U.S.C. § 1983 alleging an intentional violation of a constitutional right is brought against a particular state or local government executive branch official, or against separately incorporated local governments, money damages and prevailing party attorney fees can be awarded. The attorneys' fees are authorized by 42 U.S.C. § 1988. These money damages and attorneys fees are owed directly by the defendant in that case, although sometimes that defendant has insurance that will pay the judgment, or is indemnified by an employer. Sometimes a 42 U.S.C. § 1983 action will also include an injunctive relief claim, for which attorney fees will be awarded against the defendant in the claim, even though there is not request for money damages because only injunctive relief is sought.
In a Bivens claim which allege that a federal government executive branch official violated someone's constitutional rights (which is only available for some particular enumerated constitutional rights and not for violations of any constitutional right in general), the damages are paid by the federal government official who is the defendant in the case. But attorney fees are not available. See, e.g., Lauritzen v. Lehman, 736 F.2d 550, 559 (9th Cir. 1984).
Neither money damages nor attorney fees are available, at the local, state, or federal level, for a lawsuit seeking a declaratory judgment (which simply seeks an official court determination regarding the constitutionality of a law in a context when there is a bona fide actual dispute over its validity), or in a freestanding injunctive relief claim separate from any 42 U.S.C. § 1983 claim (e.g., in a claim alleging a violation of a state constitutional right, because § 1983 claims are only allowed for violations of federal rights), or in a claim seeking a common law writ, such as a writ of mandamus (i.e. an order for an official to carry out a ministerial, non-discretionary act), a writ of certiorari declaring that a government official's official acts were invalid in a quasi-judicial manner because the official engaged in an abuse of discretion (in connection with this question because they enforced an unconstitutional law or rule), or a quo warranto writ to determine who currently holds an elective or appointed office (in the connection with the question, on constitutional grounds). In federal court, the power of the court to issue common law writs to address unconstitutional or preempted legislation arises under the All Writs Act, codified at 28 U.S. Code § 1651, which incorporates by reference the common law of writs as federal law.
State and federal judges and legislators have absolute immunity from civil liability for their official acts (as opposed to their personal, non-job related conduct). Legislators never have personal liability for enacting an unconstitutional or preempted law. At the federal level, this is partially a consequence of Article I, Section 6, Clause 1 of the U.S. Constitution which is known as the speech and debate clause. Most state constitutions have parallel provisions applicable to state legislators.
Governmental entity damages liability for enacting an unconstitutional or preempted law is almost always limited to damages arising from the enforcement of the law, and is usually brought as a lawsuit against the official enforcing the law, and not for damages arising from the mere enactment of an unconstitutional or preempted law. Indeed, one common way for a governmental entity to avoid money damages for enacting an unconstitutional or preempted law is to have the official in charge of enforcing the law execute and file with the relevant court an affidavit stating that the official has adopted an official policy of not enforcing the law against the persons who have brought the lawsuit and people who are similarly situated. See, e.g., Brown v. Buhman, No. 14-4117 (10th Cir. 2016).
Local, state, and federal prosecutors also have absolute immunity from civil liability (other than court sanctions for unethical litigation misconduct discussed below) from their conduct in prosecuting criminal cases in court, even if the law that forms the basis of the prosecution is unconstitutional or has been pre-empted. But prosecutors have only the same qualified immunity from civil liability that executive branch law enforcement officials due for their conduct in connection with pre-litigation criminal case investigations (e.g. a prosecutor can have civil liability to a suspect for money damages for requesting a search warrant or arrested warrant from a court in connection with a crime known by the prosecutor to be unconstitutional or to have been preempted by federal law under well-established federal law).
Lawsuits directly against state governments, rather than state government executive branch officials, brought by anyone other than the federal government or another state, must be brought in state court and are governed by state law, as a consequence of the 11th Amendment to the United States Constitution. State governments are immune from civil liability for tortious conduct except in cases where the law of that state or conduct of the state in a lawsuit, or in a clear express provision of a federal statute waived that immunity (one of the most common example of a waiver of immunity by a state government in litigation is in federal bankruptcy cases where the state voluntarily files a claim seeking to enforce a debt it says it owed to it by a debtor in the case, which can open the door to setoff claims on grounds including those relevant to this question). Usually, state law does not allow for an award of money damages or attorney fees in those cases. But there are some states that are an exception to that rule in some circumstances. Some states, for example, allow for an award of damages in the case of a wrongful criminal conviction obtained because the government was attempting to enforce a preempted or unconstitutional law, and when property is taken using the state's eminent domain power in violation of the state or federal constitution.
Attorneys fees are not awardable in lawsuits brought by a state against another state in the U.S. Supreme Court, pursuant to Article III, Section 2, Clause 2 of the United States Constitution, in the vast majority of cases, and any money damages awarded are paid by the state government against which they are awarded. Of course, in those cases, all of the lawyers are salaried government employees.
The availability of damages and attorneys fees in lawsuits between the U.S. federal government and a state rather than a state official depends upon the particular statute or common law theory that forms the basis of the lawsuit. But again, usually attorney fees are not awarded against either the state or the federal government in this situation and any money damages are paid by the government against which they are awarded. Of course, again, in these cases, all of the lawyers are salaried government employees.
In the case of the federal government, or in the case of the state government in many states, claims for money damages against on grounds where the government has expressly waived its own governmental immunity, are dealt with by a special separate court of claims. But the federal government has not waived and state governments almost never expressly waive claims against themselves for damages caused by their government attempting to enforce a preempted or unconstitutional law. Pretty much the only time that this would happen would be in a case where a government unconstitutionally tried to abrogate a contract it entered into with someone, in violation of the contracts clause of the U.S. Constitution and/or parallel state constitutional rights.
Notable exceptions to the general rules
This said, the obligation to pay attorneys fees referenced above by parties to the case are for attorney fees incurred litigating in the lawsuit in question.
There are three main exceptions to these general rules.
First, one component of those economic damages can be attorney fees incurred to litigate with a third-party to the lawsuit in a different case, in any case where economic damages can be awarded for a violation of constitutional rights on any theory, e.g. in a Bivens action or a 42 U.S.C. § 1983 case. See, e.g., Zuniga v. United Can Co., 812 F.2d 443, 455 (9th Cir. 1987) (the "American rule" that each party bears their own attorneys fees in litigation does not apply in "those cases in which the attorney fees are not awarded to the successful litigant in the case at hand, but rather are the subject of the law suit itself.").
For example, suppose that someone sues a federal official in a Bivens action for unlawfully seizing and destroys evidence in another unrelated real estate lawsuit, and the destruction of those documents forces the victim of the unlawful search to incur $100,000 of attorneys' fees and litigation costs to recreate those documents with depositions of numerous witnesses in the real estate lawsuit. Those attorney fees could be recovered from the federal government official defendant in the Bivens action, even though attorney fees from the Bivens action itself could not be awarded.
Second, the judge in a lawsuit can enter an order requiring an unethical government lawyer to personally pay the attorney fees incurred by the victim of the government lawyer's misconduct in that lawsuit as a litigation sanction for the unethical conduct in which the government lawyer participated personally (e.g. by intentionally refusing to disclose documents that the government attorney admits are subject to a court disclosure obligation. Needless to say, however, sanctions awards like this are extraordinary and very rare. Sometimes, the government lawyer's employer might indemnify the government lawyer for this sanction expense, but usually it wouldn't and usually the sanction would not be covered by insurance because it is an intentional act. In federal court, sanctions against attorneys in a case are most often awarded pursuant to Federal Rule of Civil Procedure 11 and/or Federal Rule of Civil Procedure 37 and/or 28 U.S.C. § 1927. See also, e.g., Pacific Dunlap Holdings, Inc. v. Barosh, 22 F.3d 113 (7th Cir. 1994). At the state level, many states model their court rules on the federal rules of civil procedure and have parallel Rules 11 and 37, and most states also also have their own statutes sanctioning attorneys for misconduct, such as Colorado Revised Statutes § 13-17-102.
Third, a judge, can impose monetary sanctions personally on anyone whether or not they are a party to a lawsuit, or on any party to a lawsuit, who engages in misconduct in the judge's courtroom, or willfully defies a court order anywhere, because they are in contempt of court. This is done only after the proper process, which is rather complicated to explain because there are three or four different kinds of procedures that are followed in these cases based upon the specific type of misconduct involved and the relief sought against the person who allegedly engaged in the misconduct.
The example given
Say that by the time the courts sort this out and manually and
specifically issue the required rulings to free poor Jimantha, they
have already been arrested, lost their job due to not showing up, lost
their apartment due to not being able to deliver the rent check, had
all their stuff destroyed because it was in their apartment, and spent
$20,000 paying a lawyer to file the motions necessary to get the court
to act on this law specifically for them.
Wrongfully prosecuted criminal defendants have very few remedies available to them in the United States. Very few states would allow an acquitted criminal defendant to recover any of the harms arising merely from being prosecuted under a law that is ultimately held to be unconstitutional.
People have gone to prison for decades in cases where there rights were blatantly violated and gotten little relief other than their eventual release (and they're the lucky ones, the unlucky ones are procedurally barred from relief despite having been clearly wronged).
Jimantha Higgins came out much better than most by being released from custody fairly quickly because of the lawyer she hired and getting her criminal record cleared up.
But does that calculus change if the law being executed was
transparently prohibited by a higher legal authority, and everyone
involved knew or should have known that it was not actually allowed to
be executed?
Not really. Duly enacted statutes are presumed to be constitutional as a matter of law unless and until it is shown beyond a reasonable doubt that the law is unconstitutional in a court proceeding.
Also when the cops arrested them they broke their toe and they have
$2000 in medical bills. And the landlord of the apartment had to buy a
new door because the cops bashed it in.
The other damages would have to be analyzed in the context of whether the conduct involved in the arrest that caused physical injuries to the criminal defendant and property damage to the landlord would have been recoverable as an unreasonable search and seizure without regard to the defendant's guilt or innocence. Given the small dollar amounts involved there, in all likelihood, it wouldn't be economic to sue, especially given that liability for an unreasonable search and seizure might be a fairly close call here.
Generally the government isn't liable for damages caused by executing
the law.
This statement is over broad. Even if the laws are valid, they still have to be enforced in a manner that respects the constitutional rights of those involved and is not "unreasonable" given the circumstances. Police can use force to make an arrest, but only reasonable force under the circumstances.
A footnote on the collection of judgments for money damages
Collection of money judgments from individuals and private entities
When money damages or attorney fees are awarded against an individual or private entity, the money judgment from the court establishing this can be collected involuntarily from the judgment debtor by seizing property that the judgment debtor owns or by garnishing the income of the judgment debtor.
Also, these parties have to post a bond for the amount of the money judgment awarded to stay enforcement of a trial court judgment against them, pending an appeal.
Collection of money judgments from governmental entities
In contrast, when money damages or attorney fees are awarded against a government entity, the government entity needs to authorize the payment of the judgment, usually with a legislative appropriations resolution. Government entities are usually well behaved, compared to individuals and private entities, and do this within a reasonable time without being forced to involuntarily to satisfy the judgment with a seizure of property. But if a government entity fails to pay a final money judgment the procedure for forcing it to pay are complicated, slow, and rather fraught, but this process usually doesn't involve involuntary seizures of government property.
States can't file for bankruptcy, but in rare cases, collection of a money judgment against a local government can be secured as a part of a Chapter 9 bankruptcy case in federal bankruptcy court.
Governmental entities, unlike private parties, generally don't have to post a bond to stay collection of money judgments against them pending an appeal of a trial court judgment.
Claims allowed by a final order of a court of claims are typically paid as a matter of course, sometimes even without further appropriations legislation from the government entity in question. Some governments with a court of claims even waive the right to appeal judgments against them from that court, so that only unsuccessful claimants can appeal from the court of claims' judgment.