A smart contract is a self-executing contract with the terms between involved parties being written into code.
A simpler definition is that a smart contract is a digital vending machine.
You put something in and you get something out based on transparent, pre-defined rules. The same thing will happen every time; you can look into the vending machine to see what you're going to get; the vending machine doesn't care who you are; there's no middleman you have to place your trust in.
A few qualities of smart contracts that make them a desirable tool are that they are (or are at least capable of being):
It's obvious why we would want to interact with deterministic, cryptographically guaranteed, and transparent systems. You want reliable outputs, you want a guarantee from a completely objective source such as math, and you want a clear view of the mechanics, respectively.
What's not quite as obvious is why we want our interactions to be permissionless, peer-to-peer, or trustless.
Permissionless: It's great to only share your data with companies/organizations that you trust to manage it properly, but it's even more secure to not share that data at all. In a permissionless system, you don't need to provide credentials to interact and have the integrity of that data potentially compromised. There is no opportunity for discrimination or targeted censorship if everyone is pseudo or fully anonymous.
Peer-to-peer: Middlemen provide no value other than a secure medium through which to transact. If that medium is already available, the only purpose they serve is to extract value, collect data, and slow down the process. Removing middlemen makes transactions cheaper, more private, and faster.
Trustless: You're probably thinking "I'd rather not interact with anyone or anything that I can't trust, so why does it need to be trustless?" and you've got a good point. However, this truly enables P2P environments to flourish, by not needing to trust another party to transact with them. Trusting that someone won't break the rules or rip you off is not nearly as good as knowing that they can't be a bad actor, due to the terms being laid out in an immutable contract with authority to execute on its own predefined terms.
The difference between smart contracts and paper-based agreements is the difference between can't and won't.
A paper-based agreement is the terms & conditions for a centralized company, a law, or another relationship with a centralized entity; all of which are subject to change based on trust and interpretation. Decentralized environments don’t fall victim to those restrictions.
Centralized companies can change their terms of use at any time and redefine the parameters in which you have to operate, which is definitely not favorable if you’re a user that was under the impression the guidelines at the time of signing up would remain.
An example of this is using Robinhood as a brokerage and operating under the assumption that you can buy and sell stocks of your choice, only to find out Robinhood would decide to freeze trading on certain assets in 2021 due to their own personal interests.
When interacting in decentralized environments with smart contracts, there is no need to trust the other party to be a good actor because they cannot break the terms.
Someone wants to freeze your money, can't vs won't. Some scammer wants to rip you off on a transaction, can't vs won't. Bank wants to restrict my ability to spend my money on a cause they don’t like, can't vs won't.
Paper-based agreements meet none of the aforementioned qualities of smart contracts. They are unreliable, opaque, involve middlemen, and heavily rely on trust.
Smart contracts need to exist in a decentralized ecosystem by design, so they exist on blockchains such as Ethereum or Solana. Not every network has the functionality to support smart contract execution, such as Bitcoin.
Ethereum is the most popular smart contract network and Solidity is the most popular programming language for smart contracts on Ethereum.
With Ethereum and Solidity, you can deploy contracts to a decentralized blockchain where you or anyone else can interact with it, with complete trust and transparency in the outcome. Everything is open-source by default, so it’s easy to see what information is collected; this differs from our interactions with centralized companies.
You can deploy very simple and modular smart contracts, referencing them in more complex contracts, or you can deploy large specific ones that may only serve you for one specific use-case. It’s all up to you.
Because they’re hosted on a decentralized blockchain, interacting with them costs gas; in the case of Ethereum, you have to pay Ether to deploy a contract or interact with an existing one. You’re writing to the network whenever you interact with a contract, which means you’re taking up space in the next block that is to be minted, so you have to provide gas for miners to include your transaction.
For example, storing a value in a smart contract would cost gas. Reading that value would not.
How much gas the transaction costs depends on the contract. The more efficient the code is, the less is actually being written, the less space is being taken up, and the less gas you’d have to pay. Better code means a better experience for you and everyone else interacting with your contract.
And then security is the other side of the coin. Gas efficiency is great, but not at the risk of security. You don’t want people interacting with your contract in ways you didn’t intend, so you can’t go totally barebones on the security to stop re-entrancy attacks and have permissions checks for certain interactions.
Remember, once a contract is deployed then it's immutable. You can't come back later to fix an exploit; you have to deploy again.