The mining of bitcoin is done to secure the bitcoin network. Securing the network means authenticating bitcoin transactions by consensus of the bitcoin miners.
When you buy a cup of coffee using bitcoin, the transaction is broadcast to every bitcoin miner on the bitcoin network. The miners simultaneously start the process of authentication. The transaction is presented as a mathematical problem which must be solved to authenticate the coffee transaction.
The miners compete to solve this problem. The miner who solves it first gets rewarded through bitcoin. Mining is one way of getting bitcoin. You can also get bitcoin by buying it or having someone send it to you.
The miner who solves the problem First helps to validate the coffee buying transaction. Every transaction made using bitcoin is validated using this method.
The process of mining uses up a lot of electricity and requires special computer hardware. The electricity bill for mining a single bitcoin could be as much as the bill for the entire month for a small household.
Only 21 million bitcoin will ever be mined. The bitcoin mining process is designed so that it becomes increasingly more difficult to mine bitcoin as the 21 million figure is approached. The inherent difficulty is to mimic the scarcity property of a currency.
If you have any questions please ask in the comments section.