The basis of Bitcoin and other cryptocurrencies is a blockchain - a database where all the transactions that ever happened are written in.
Because of the data in blockchain your bitcoin wallet can calculate how many bitcoins you have. It searches for all the input and output transactions from and to your address and calculates the current balance (note that not every cryptocurrency works this way - Ethereum keeps a list of balances instead).
Transaction usually is when a person A sends bitcoins to person B. Each transaction has three main parts: input, value and output:
What is the role of input here? Each new transaction has to have a basis in one or more of the previous transactions. So if person A wants to send, let’s say 1.5 bitcoins to person B, s/he had to have received at least this amount to her/his bitcoin address before (in one or more transactions).
When you take a look at transactions on a blockchain, you might notice something weird happening. Let’s say person A received 2 bitcoins from person C. Now person A wants to send 1.5 bitcoins to person B. The transaction will in fact use both 2 bitcoins and send 1.5 bitcoins to person B and 0.5 bitcoins to some "change address" and eventually return this amount back to person B. This is a feature that not all cryptocurrencies have.
Miners check if transactions are valid and put a bunch of transactions into a block. Each miner creates a bit different block of transactions (different tx in it), but only one block can be approved by the whole network at the time and put into the blockchain. So how can miners decide which one to choose?
The answer is by a competition. Each miner creates their own block of bitcoin transactions. After this they create a hash of their block. Then they pick a random number (called a nonce), combine it with the block hash and create a new (final) hash out of it.
The goal is to get a final hash that has a certain amount of zeros at the beginning, like this:
Let’s say that the goal is to find a hash with 12 zeros at the beginning. Miners would try out many attempts and change nonce every time until someone finds the right hash. This miner then sends a proof to all the other miners and is proclaimed a winner. His/hers block can be added to the blockchain and the miner receives a reward for winning.
After this the whole competition repeats for each new block. Obviously all this process is being done automatically by the mining software.
As we said at the beginning, each miner has to check every transaction it includes in their block to see if it’s valid. Miner also has to check the previous block if all the transaction in it are valid. If they aren’t, it has to reject the false transaction or the whole block and notify the others.
It happened in the past that some miners did not validate the previous block and the transactions because they wanted to save on their resources. For some it resulted in adding a block after an invalid block which later resulted in both blocks being rejected by the network (even though the second block might not have had any false transaction in it). This way the reckless miners suffered quite big losses.
(This article was also posted on cointldr.com)