What is a smart contract in a blockchain?

In plain words
A smart contract is a program that is written right inside the blockchain and executes pre-set rules by itself, without an intermediary. The rule is simple: if one thing happens, the program automatically does another. Once the condition is met, the outcome usually can no longer be changed.
The easiest way to picture it is a vending machine: you drop in a coin, and it hands you a can. The machine needs no salesperson, it carries out its rule by itself, and the rule is the same for everyone. A smart contract is the same kind of machine, only for digital money and data, and its rules are visible to everyone.
The word contract is misleading here. This is not a legal agreement with signatures, but program code — a rule that the network carries out by itself.
Deeper
How it differs from an ordinary program
An ordinary program runs on someone’s server, and the owner can quietly change it or switch it off. A smart contract is published on the blockchain, its rules are carried out by the network, and changing them quietly after the fact is much harder. There is an important caveat here: not all contracts are unchangeable, and not all code is open. Some contracts are built to be upgradeable, some have admin keys and a way to pause the contract, and the source code is not always visible. So being unchangeable and being transparent are properties of a specific contract worth checking, not a given for every one.
Execution is paid for not by the contract itself, but by whoever calls it, that is, the sender of the transaction. They pay a network fee. On Ethereum this fee is called gas, which the article what is gas is about. In some setups another party can pay the gas for you.
The Bitcoin network can check simple conditions for transfers, but the idea of general-purpose contracts, where almost any logic can be programmed, was brought to the masses by Ethereum. That is why most tokens, stablecoins such as DAI, NFTs, and various apps inside a network run precisely on Ethereum and networks like it.
What is built on them and where you meet them
Much of what a beginner runs into runs on smart contracts under the hood. This includes a stablecoin, a swap of one coin for another without a company behind it, and the issuance of a token or an NFT. You may never once see the word contract and still be using them.
Hence an important practical point: a smart contract carries out what is written in it, not what you had in mind. When you grant a contract access to your coins, an action called approve, you are trusting its code. So it is worth understanding exactly what you are allowing. The article wallet connection safety goes into this.
Honestly about “code is law”
“Code is law” is a common way to describe smart contracts: once the rule is written in the code, it runs on its own, without anyone’s decision. This self-execution has two sides.
The good side is that if the rule is written and the contract cannot be changed, no one can change their mind and fail to deliver what was promised — neither the creator nor the platform. The flip side is that the code carries out exactly what is written in it, including a mistake: if the contract has a bug, it will run to the letter, funds can go to the wrong place, and usually there is no one to roll it back, since a transfer on the blockchain cannot be undone.
“Code is law” itself is not always true, either. Many contracts have admin keys, meaning the creator can step in; some have a pause button; and sometimes a contract depends on outside data fed in by a so-called oracle — a program that supplies real-world data to the blockchain, such as an exchange rate. All of this can change or halt how the contract works.
That is why serious projects commission an independent review of the code, called an audit. An audit lowers the risk of bugs, but gives no hundred-percent guarantee: holes are sometimes found even in audited code.