I know that you get bitcoins by mining, but mining is just lottery, right? What I want to know is what miners do to deserve bitcoins, or the money is just randomly given without requiring someone to work for it?
Miners keep the bitcoin network running
Miners create new bitcoins, but
Bitcoin network adjusts its own mining difficult so that more there are miners less bitcoins each miner receives. This keeps the money supply steady (not inflatory - you simply cannot "print more bitcoins" like you do with fiat currencies as USD).
Mining requires a lot of power, so miners face the rising cost of electricity and how much you need to invest into mining equipment to have your mining operations profitable.
Miners also confirm bitcoin network transactions. When you send someone bitcoins, miners check that your transaction is genuine and not double-spent attempt.
Here is also related question How does a difficulty increase affect a miner's income?
Bitcoin is a payment system that has a number of characteristics that make it superior for some applications, e.g. international money transfer, online commerce, and peer-to-peer money transmissions.
Mining serves the purpose to slowly spread sufficient liquidity to the community, in order for it to be usable. Also, mining serves the stability of the Bitcoin network by validating transactions, and protecting against fraud. People giving their electricity and hardware are reimbursed by the miner's reward, creating an incentive to do what is good for the network. In exchange every user gets to use the Bitcoin network.
The mining reward is given out in a kind of lottery, but since it is being given out so often and through the miners forming mining pools, the reward is actually spread pretty evenly to all miners.