In cryptocurrencies, mining is the process of completing a new block of transactions and adding it to a blockchain. Miners are nodes in cryptocurrency networks that select transactions from the mempool, validate that the transactions follow all protocol rules and include them in the created block.
Mining is typically a decentralized process. A successful valid block formation is a random event, the probability of which is proportional to the computing power of the miner's hardware. The reason why miners are motivated to commit their resources to mining is that they are rewarded by newly created cryptocurrency in each block they successfully create together with transaction fees from each transaction they add to valid block. Mining thus also constitutes the default way of decentralized cryptocurrency initial distribution. However, mining is not primarily process for creation of new bitcoin but serves as a mechanism by which is the security of the network decentralized.
Mining is a competition between miners (or typically mining pools). Every miner tries to solve a difficult mathematical problem based on a cryptographic hash algorithm. A solution - Proof-of-Work - is included in the found block and provides proof that the miner spent the necessary power or computing effort. SHA-256 hash function is used in the bitcoin mining process. Mining is the process of hashing the block header repeatedly, changing one parameter, until the resulting hash matches a specific target.
Mining - hashing repeatedly the block header to meet the target
Miner nodes which want to win the competition in finding next block has to prepare so-called candidate block - it means it is not valid until miner finds the valid Proof-of-Work. Candidate block contains coinbase transaction (which contacins block reward for miner), transactions that are waiting for their first confirmation (picked up from the mempool), block header which consists of version, previous block hash, merkle root, timestamp, target and nonce.
Miner's hardware starts to hash the block header repeatedly with SHA-256, only with different nonce every time. Nonce provides additional data to block header, which will cause different hash every time the block header is hashed. When this hash meets target the candidate block becomes valid and can be broadcasted to the network as a new block.
The target is a challenge, it means that the hash of the new block header has to meet certain criteria. The probability that the hash will meet the criteria is very low, so, typically, many hashes have to be produced by miner's hardware until the one which meets the criteria is found. A lower target means it is more difficult to find a hash below the target. The target and difficulty are inversely related.