Bitcoin miners listen to transactions that are broadcast to the network. When they receive a transaction, they verify that the transaction is valid and that all the inputs to the transaction are fully used. For example, if Amy wants to send 1.5 BTC to Bob, she needs to select outputs from previous transactions that total more than 1.5 BTC. then define a transaction that outputs 1.5 BTC to Bob and returns the rest to herself (the miner claims any remaining BTC that is not output as a transaction fee). If any input has been used or the transaction is otherwise invalid, the miner will drop it. If everything is in order, the miner may include the transaction in the block he is mining.
The miner then groups the transactions into pairs and hashes them together (including a special transaction called a coinbase transaction). Coinbase transaction is a transaction that sends 25 BTC (for now) to the miner as a reward for processing the block. They then put pairs of hashes together until they finally get a single hash of the entire transaction block. This structure is called a Merkle tree and it is the proof of each transaction in the block. The last hash is called the Merkle root.
The miner mixes the Merkle root, the hash of the previous block in the blockchain, and a nonce. Miners will actually try billions of nonce, trying to find one that makes the hash of the current block they are mining start with a certain number of zeros (depending on the current difficulty). For most Merkle roots, there isn’t a nonce that matches the difficulty, so miners will go back and change the Merkle tree by adding transactions or faking coinbase transactions. Changing the tree means that there will be a new Merkle root and they can try all possible nonce again to try to find a solution.
If they find a nonce that creates a block hash that matches the difficulty, then they broadcast it to the network and include it in the blockchain. They collect their reward as the coinbase transaction (which created 25 BTC and put it in their wallet) is now committed to the blockchain.
Sometimes, two miners find a solution at roughly the same time. In this case, the other miner will choose the solution they received first and try to build on that. Eventually, the split is resolved by the branch on the chain that has the most computed hashes. Failed branches are dropped and the transactions on them are invalid (but they may be included in another branch anyway).
Bitcoin mining is a process of verifying and recording new bitcoin transactions. Miners compete to complete cryptographic tasks to process transactions. Bitcoin mining helps maintain the blockchain history and creates new bitcoins. Miners are rewarded with transaction fees and newly created bitcoins.
This is roughly how mining works!