Computers around the world 'mine' for bitcoin by competing with each other to solve complex cryptographic problems. People make transactions on the Bitcoin Blockchain constantly, and all transactions need to be recorded in the public ledger. In centralized systems, there is a single authoritative entity tracking all transactions. If the ledger's keeper goes offline or is hacked, the whole system stops working. The Bitcoin ledger, called the Bitcoin Blockchain, is distributed and maintained by a multitude of independent entities all over the world. It guarantees unmatched resilience and censorship resistance of Bitcoin compared to centralized ledgers.
As there is no single authority in Bitcoin, independent transaction processors need to agree on the common ledger. The Bitcoin Blockchain manages this by collecting freshly made transactions into lists called blocks. Whenever a new block of transactions is created, it is added to the Bitcoin Blockchain, creating an increasingly lengthy list of all the transactions that ever took place on the Bitcoin network. It's the miners job to confirm created blocks and secure the block contents by solving a cryptographic problem. Once the problem is solved, the block is added to the public ledger of all recorded transactions. That block is broadcast over the Bitcoin network, allowing all participants to update their local copies of the ledger.
How can we be sure that the Bitcoin Blockchain is never tampered with? This is where the miners come in.
When a block of transactions is created, miners work to solve the cryptographic problem that would allow the block to be added to the Bitcoin Blockchain. To do so, they take the information in the block, and apply a mathematical formula to it. This formula turns the information from the block into a short, random sequence of numbers and letters. This is known as a cryptographic hash. A hash of each block must satisfy a constraint: the hash treated as a big integer number should be less or equal to the current network difficulty target. The target is dynamically changed so that it takes 10 minutes on average to find a valid block.
HASHES HAVE SOME INTERESTING PROPERTIES. IT'S EASY TO PRODUCE A HASH FROM A COLLECTION OF DATA LIKE A BITCOIN BLOCK, BUT IT'S PRACTICALLY IMPOSSIBLE TO WORK OUT WHAT THE DATA WAS JUST BY LOOKING AT THE HASH. IF YOU HAVE THE HASH OF SOME DATA, IT'S GENERALLY IMPOSSIBLE TO WORK BACKWARDS AND FIND OUT WHAT THE ORIGINAL DATA WAS.
While it is very easy to produce a hash from a large amount of data, each hash is unique. If you change just one character in a block of transactions, the hash of the block will change completely unpredictably. Because of this property, it is difficult to generate a block with a valid hash. The best you can do is to iteratively change the contents of the block and see if its hash matches the validity criteria. It is a very computationally intensive task; as of the beginning of 2016, to generate a valid Bitcoin block, one needs to perform more than 100,000,000,000,000,000,000 iterations on average. There is no surprise that Bitcoin mining is no longer performed on personal computers. Instead, miners use specialized hardware.
When hashing a block, miners don't just work with the transactions. They also use the hash from the last confirmed block in the Bitcoin Blockchain; that is, each block references the previous one. Because each block's hash is produced using the hash of the block before it, it becomes a digital version of a wax seal. It confirms that this block – and every block before it – is legitimate. With each block piled on top of a certain block of transactions, the confidence in the block increases.
If a bad actor tried to fake a transaction by changing a block that had already been stored in the Bitcoin Blockchain, this would change that block's hash. If someone checked the block's authenticity by running the hashing function on it, they'd find that the hash was different from the one referenced by the next block. The attacker would need to change all blocks after the block he wants to modify. Moreover, he would need to generate a longer new chain of blocks than all other miners in order for the network to accept the attacker's change. This would require tens of millions of US dollars in investments.
To earn bitcoin, miners work to be the first one to solve a block hash problem that fits within the Bitcoin protocol. Every time someone successfully solves the problem, they get a reward, which consists of two parts: newly created bitcoin and fees from the transactions included into the block. As of the beginning of 2016, each block creates 25 new bitcoin, and transaction fees are about 0.5 bitcoin.