The Object and Purpose of a Contract Must be Legal and Moral
Basic contract law principles require that the object and purpose and terms of a contract be lawful and moral if the courts are to uphold and enforce the contract. This requirement is known simply as the element of 'legality' and is one of the six primary elements required for the formation of a legally binding contractual agreement. Without the six primary elements, the courts should treat the 'purported' contract as exactly that, 'purported' or at best, a failed attempt to establish a legally binding contractual relationship. Note that such inherently implies that where an attempt to contract fails, and therefore the very existence of any legally binding contract failed to occur, such a contract is therefore non-existent as a 'contract' never happened, legally speaking. With this said, it is common that laypeople will attempt to establish an enforceable contractual arrangement, and cross the line into illegality, without intentions to break the law and therefore with intent and belief that the attempt to contract is legitimate. As simplistic examples, it is easily recognized by most reasonable people that two people are unable to enter into an enforceable contract when the purpose of the contract is to illicitly smuggle drugs in that if the drug smuggler goes unpaid, the drug smuggler will be unable to attend court to seek an Order instructing payment as agreed per a 'contract' to smuggle drugs. However, the same principle applies when a homeowner and a landscaper attempt to enter into an enforceable contract to install a tree in front of the home but upon land that is municipally owned. If the landscaper installs the tree, per an agreement with the homeowner, and the municipality removes the tree advising that such was an illegal act, the landscaper will have a very difficult effort seeking court assistance for payment from the homeowner.
The Law, jurisprudence
Cases on the point of law that require legality of object and purpose are plentiful and are often available to address a specific issue of illegality or immorality as well as the general principle which was well outlined within Complete Access Lift & Mobility Ltd. v. Riggi, 2010 CanLII 100648 where it was stated at paragraph 116:
Ex turpi causa is a venerable old legal maxim with a rather chequered history and a somewhat tenuous claim to accurately describing current legal practice. Loosely translated it means that the Courts will not enforce claims that arise out of illegal or immoral conduct. It has been all-but abolished in the context of tort law in Canada: Hall v. Hebert, 1993 CanLII 141 (SCC),  2 S.C.R. 159, but survives in the law of contracts: the law will not enforce a contract involving the commission of an illegal act.
The various types of contracts that are viewed as illegal where outlined in summary within Montreal Trust Co. et al. v. Stanrock Uranium Mines Ltd., 1965 CanLII 298 (ON SC) where it was said:
The contracts that are illegal in the strict sense are the following: a contract to commit a crime, a tort, or a fraud on a third party; a contract that is sexually immoral; certain contracts contrary to public policy, such as contract to the prejudice of public safety; contract to the prejudice of due administration of justice; contract that tends to corruption in public affairs; contract to defraud the revenue, or one prohibited by statute.
A common illegality of contract (as above failure to enter into a legitimate bona fide and therefore enforceable contract) situation that arises are those attempts to contract where an object or purpose or term within the agreement is to avoid taxation, essentially a defrauding upon the government such as that which occurred in Jabbaz v. Mouammar, 2003 CanLII 2317 (ON SC) at paragraph 21:
... it is well settled that a contractual provision whose ultimate design is to perpetrate a fraud on a public taxing authority is contrary to public policy and unenforceable (see Alexander v. Rayson (1935),  1 K.B. 169 (Eng. K.B.); Miller v. Karlinski (1945), 62 T .L.R. 85 (Eng. C.A.); and, Napier v. National Business Agency Ltd.,  2 All E.R. 264 (Eng. C.A)). If the judge's finding that the indemnity provision was inserted in the agreement between the parties with the ulterior purpose of evading tax is supportable, then its objective can only be viewed as aimed at defrauding the provincial government. In that case, precedent clearly justifies declaring the provision unenforceable on the ground that it is, as the judge held, contrary to public policy.
It is of particular interest, per Complete Access Lift as above while citing Hall v. Hebert,  2 S.C.R. 159, that the ex turpi causa doctrine applies only to the unenforceability of contract without application in tort law. Accordingly, it appears that a contractor and property owner, such as a homeowner, could attempt to form an agreement for which the terms are unenforceable due to illegality; however, for tort law matters, such as negligence, arising from the attempt to contract, the right to pursue claims within the courts remains.
Contracts with an object or purpose or terms that are illegal or immoral may, a likely will, be deemed unenforceable; however, tort law issues and perhaps equity law issues such as unjust enrichment claims, may remain enforceable.