This article assumes that you know what are both the bitcoin and the blockchain. If that's not the case, please take a look at the following articles:
As we already know, all the nodes of the network keep validating and broadcasting transactions. However, there are certain nodes that, apart from doing this, also dedicate processing power to undertake a search of a precious number: the also called miners.
Let's think about the concept of mining as the search of something that is buried. With this idea in mind, imagine the following: we pick a huge mountain of numbers and we assert that in that mountain there is a buried number that meets a certain condition*, although we don't know the number. The goal is to find it, and we can't know if a certain number is the right one until we test it, so the practice of processing every number to determine if it's the right one is what we could call mining.
* The details about the condition the number needs to meet will be detailed in next sections of this article.
The above described search is important because we can stablish the size of the mountain to determine how much processing power will be needed to find the number. That is what we call the difficulty of the block and is used to determine how much time, on average, will take mine* it. The more available processing power the higher needs to be the difficulty to have an average solving time of 10 minutes, which is the block time designated for bitcoin. If you are wondering why more processing power would become available, just keep reading.
* We say a block has been mined when the correct number has been found.
There is an incentive to dedicate processing power to bitcoin mining: the node which mines the block is the one who writes it on the blockchain, and also receives a reward in form of newly issued coins. At the time of writing this article the reward consists of 12.5 BTC per block plus mining fees*, and the processing power dedicated to bitcoin mining is ridiculously huge: 119 x 1018 numbers are being tested every second. This is important because it makes costly to be the one who writes a new block. Let's see why it's important.
The blockchain is already secure when it comes to the fact that only the owner of a certain bitcoin address will be able to spend the coins it may have. However, there is still a vulnerability that is theoretically possible. Imagine the following scenario: a user spends 10 BTC in purchasing something, then aims to delete that transaction from the blockchain thus recovering the money. In order to do that, the user should be the one who writes the new block, which we've already seen that is not an easy thing. Even worse, if the transaction was already written in a block, the user alone should re-solve that block in order to delete a transaction. Think about it: a sole user must solve a problem that was designated for the whole network, while the network is working on its own by mining more blocks. It is important to note that some sort of race would be occurring here.
In case of an attack to the network, there would be two different transaction histories. When a node needs to determine which one is the valid, it follows a simple criterion: the longest chain. Given that both groups will work with the same difficulty, it is expected that the group with more processing power will mine blocks faster and will generate a longest chain, thus becoming the reference chain followed by every honest node. So in order to succeed with this kind of attack, you would have to gather a processing power at least equal to the gathered by honest nodes. It is important to note that it is not only extremely difficult nowadays (and more difficult every day), but also impractical. If you have the processing power to even consider this kind of attack, it is incomparably more profitable to be an honest node thus receiving rewards in form of a trusted asset than trying to "unspend" a transaction.
The whole concept of bitcoin mining relies on the search of a mysterious number; we've defined the search as a giant mountain containing a number with certain properties, but we were just using a metaphorical explanation to conceptualize something that is a bit more abstract, and the time has come to put more light into this.
Let's take a look at what hashing is and why it's important here.
Imagine the following: we assign a number to each letter, based on the position it occupies in the alphabet, so A=1, B=2, C=3, and so on. Now let's say that "hashing" a word means to sum all the numbers of every letter so the resulting "hash" would be just that sum. It would imply that the same word would always produce the same hash. However, we can imagine different words producing the same output: the easiest way is to think about two words which use the same letters in a different order. But when it comes to the hashing function used in the blockchain, things are far more complicated at a computational level and it becomes extremely hard to reach the same hash with two different pieces of information.
Every block has a header that is generated at the moment of its creation, and that header is used as a base to stablish the requirements of the number to be found. That is because we are going to pick the header of the previous block and we are going to combine it with a nonce, then we will hash that combination. That nonce will be the number we are looking for if the resulting hash starts with a certain amount of zero bits. The amount of zero bits required is used to determine the difficulty of the block.
All the exposed information let us conclude that the bitcoin network is being run by following these steps:
So this is how the new bitcoins are issued and how at the same time this issuance serves as a method to keep the network secure. As mining nodes are incentivated to be honest nodes while fraudulent nodes are highly disincentivized, the processing power being dedicated by the honest nodes to bitcoin mining is growing over time, making the bitcoin network stronger and stronger every day.