1. Recognize that courts will not enforce illegal bargains.
2. Know that there are exceptions to that rule.
Effect of Illegality
The general rule is this: courts will not enforce illegal bargains. The parties are left where the court found them, and no relief is granted: it’s a hands-off policy. The illegal agreement is void, and that a wrongdoer has benefited to the other’s detriment does not matter.
For example, suppose a specialty contractor, statutorily required to have a license, constructs a waterslide for Plaintiff, when the contractor knew or should have known he was unlicensed. Plaintiff discovers the impropriety and refuses to pay the contractor $80,000 remaining on the deal. The contractor will not get paid.  In another example, a man held himself out to be an architect in a jurisdiction requiring that architects pass a test to be licensed. He was paid $80,000 to design a house costing $900,000. The project was late and over budget, and the building violated relevant easement building-code rules. The unlicensed architect was not allowed to keep his fee. 
As always in the law, there are exceptions. Of relevance here are situations where a court might permit one party to recover: party withdrawing before performance, party protected by statute, party not equally at fault, excusable ignorance, and partial illegality.
Party Withdrawing before Performance
Samantha and Carlene agree to bet on a soccer game and deliver their money to the stakeholder. Subsequently, but before the payout, Carlene decides she wants out; she can get her money from the stakeholder. Ralph hires Jacob for $5,000 to arrange a bribe of a juror. Ralph has a change of heart; he can get his money from Jacob.
Party Protected by Statute
An airline pilot, forbidden by federal law from working overtime, nevertheless does so; she would be entitled to payment for the overtime worked. Securities laws forbid the sale or purchase of unregistered offerings—such a contract is illegal; the statute allows the purchaser rescission (return of the money paid). An attorney (apparently unwittingly) charged his client beyond what the statute allowed for procuring for the client a government pension; the pensioner could get the excess from the attorney.
Party Not Equally at Fault
One party induces another to make an illegal contract by undue influence, fraud, or duress; the victim can recover the consideration conveyed to the miscreant if possible.
A woman agrees to marry a man not knowing that he is already married; bigamy is illegal, the marriage is void, and she may sue him for damages. A laborer is hired to move sealed crates, which contain marijuana; it is illegal to ship, sell, or use marijuana, but the laborer is allowed payment for his services.
A six-page employment contract contains two paragraphs of an illegal noncompete agreement. The illegal part is thrown out, but the legal parts are enforceable.
There are a number of exceptions to the general rule that courts give no relief to either party to an illegal contract. The rule may be relaxed in cases where justice would be better served than by following the stricture of hands off.
1. When, in general, will a court allow a party relief from an illegal contract (or bargain)?
2. A and B engage in a game of high-stakes poker under circumstances making the game illegal in the jurisdiction. A owes B $5,000 when A loses. When A does not pay, B sues. Does B get the money? What if A had paid B the $5,000 and then sued to get it back?
 Pacific Custom Pools, Inc. v. Turner Construction, 94 Cal. Rptr. 2d 756 (Calif. 2000).
 Ransburg v. Haase, 586 N.E. 2d 1295 (Ill. Ct. App. 1992).