A smart contract is a computer code running on top of a blockchain containing a set of rules under which the parties to that smart contract agree to interact with each other. If and when the pre-defined rules are met, the agreement is automatically enforced. The smart contract code facilitates, verifies, and enforces the negotiation or performance of an agreement or transaction.
Let take an example how Smart Contracts works:
Suppose John wants to sell his car through Smart Contracts. And Lisa wants to buy the car.
John leaves his car and key in garage locked with a smart contract controlled smart lock. The car has its own blockchain address (public key) 123456 stored on the blockchain.
John wants to sell his car. He identifies himself with his blockchain address (public key) ABCDEF and uses a smart contract to define the terms of the sale signing it with his private key.
If $20,000 were sent to my account number ABCDEF then automatically transfer car ID 123456 as well as grant smart lock access to the account from which the money has been transferred
Lisa wants to buy a car. She finds John's car listed on the internet. She signs the contract with her private key transferring $ 20,000 from her blockchain address (public key) ZAE1234 to John's blockchain address ABCDEF.
The smart contract is verified by each node on the blockchain network checking if John is the owner of the car and if Lisa has enough money to pay John.
If the network agrees, that all conditions are true, Lisa automatically gets the access code to the smart garage lock. The blockchain registers Lisa as the owner of the car. John has $20,000 more in his account, and Lisa $20,000 less.
Lisa can now pick up her car by unlocking the smart lock with her private key.