On a public chain, anyone can read and write data. Reading data is free, but there is a cost to write data to the public chain. This overhead helps to stop spam and protects its security through payments. Any node on the network (each connection device containing a copy of the account is called a node) can participate in the protection of the network by means known as mining. Because mining requires computing power and electricity, the miners’ services need to be compensated. This is also the reason for the miner’s fees.
The miners preferentially package gas with reasonable gas prices. If the miner’s fee paid during the transaction is out of gas, the transaction may not be packaged by the miner, resulting in a transaction failure.
Ethereum's transaction costs = gas quantity * gas price (gas unit price, quoted in Ethereum)