A contract is illegal if it involves doing something that is a criminal act or a civil wrong, or against the public good. For example, it is an offence to sell a firearm to a person not licensed to hold one, so a contract to sell a firearm in these circumstances is illegal. A contract whose purpose is to get the party to it to break another legally binding contract that the party has made already is also illegal.
Courts will not enforce an illegal contract. Money paid or property transferred under an illegal contract cannot normally be recovered. There are exceptions however. For example, where a contract is made illegal by a statute passed for the protection of a class of people, a member of that class can get back money paid or property transferred by her or him under the contract.
The law about illegal contracts is very complex. If there is any doubt about the legality of a contract, seek legal advice.