You gain badges for winning against gym leaders in gyms, as you have the badges you will get Damage bonuses for different types of pokémon depending on what badge you have.

A list of the available badges:

For information about where to gain all the badges, see their individual wikia pages.