Crypto Mining
In short: In crypto mining, computers solve cryptographic tasks to confirm new blocks of a blockchain and receive coins for it.
In more detail: This method is called proof of work. Whoever solves the task first may append the next block and receives the reward.
In Depth
Hardware
Bitcoin is now mined almost exclusively with specialised ASIC devices. Other coins (for example Monero) are designed for normal CPUs and GPUs.
Energy
Proof of work consumes a lot of electricity. Ethereum switched to proof of stake in September 2022 (“The Merge”) and greatly reduced its consumption.
Legal and illegal
Mining for yourself is legal (mind electricity costs and taxes). Using other people’s computers is a criminal offence, see cryptojacking.
See also: Cryptocurrencies, Bitcoin, Ethereum, Cryptojacking