The bunker is more or less a very big and durable piece of wall equipped with a machine gun and even capable of fighting vehicles.
Bunkers can be built immediately at the beginning of the game, as long as one has a construction yard. Their main purpose is to defend the base from incoming infantry against which it is very effective. This also allows a player to keep the enemy from scouting one's base, therefore keeping them in the dark from what strategy the player is going for.
Due to their high durability, they are also useful for tanking damage for the much less armored Rocket Towers making them more effective and for keeping Chameleon away from one's base, as they will reveal hidden units directly besides it.
They can also be used to stall against tower rushes as Sniper Towers need a significant amount of time before they are able to melt through a Bunker while also keeping the tower rusher from attacking your base with early game units.
It is also a very common strategy to place Bunkers on top of hills to get vision on high ground.
Even though they are a good way of securing one's base and have a lot of uses, they should not be built too soon, as they can slow down the base development in the early game. In most cases it is best to build 1 or 2 after one has already gone for HQ2 and built the additional supply centers. When going for a fast early game rush it is even advised to not build them at all.