Farming is the act of a hero killing multiple creeps to gain minerals. Certain heroes have abilities which aid them in farming. Farming early game will allow you to purchase better items, making your late game stronger. Later in the game, farming opportunities will be fewer.
Farming is an essential component of early-game laning. Heroes try to out-farm the enemy and "win" the lane.
How to Farm
The aim is to achieve more "CS", which stands for "Creep Score", which represents how many minions/monsters you have killed. You only receive minerals and CS for "last-hitting" the minion/monster. This means that you should always try to get the killing blow on minions.
This is the main phase for farming. Many heroes have abilities which aid farming, either by making it easier to land the killing hit.
It is harder to get CS when your lane is pushed to your tower, since the tower's attacks are stronger than your own, causing it to kill many minions. On the other hand, you can push the lane to the opponent's tower, where they will lose CS. However, this is risky, as it opens the lane up to ganks.
When teamfights begin, minerals will come from killing champions, lane minions, and clearing the jungle monster camps. When possible, lanes can still be farmed, though your absence can hurt your team in a teamfight.
Now the game will mainly focus on teamfights, meaning that farming is secondary. Despite this, creep waves will slowly accumulate (usually in side lanes where teamfights are not taking place). These can provide large gold/experience boosts, though as stated before, your absence can potentially hurt your team.