You get them from cards, which then can be crafted into the badge, or from completing tasks. For the cards, you need to trade or buy the cards you don't have from the market. For the tasks there are a variety of things you can do to get them, such as, voting for a game of the year nominee or from just having an account of a certain age (you get a new badge every year on the date you created your account). Each badge has xp, and your Steam profile has a level, each level requires xp. Every 10 levels you unlock things for your profile like, the ability to have more friends, "showcases" on your profile and clout.
However, the true reason for all this is a bit more sinister. People spend time or money to upgrade their profile and their presence on the platform. Which means people tend to prefer their games on Steam as opposed to other platforms, because on Steam they are someone while elsewhere they are not. This is why a lot of people hate other platforms, especially Epic.
You can also sell you cards on the market, however this money can only be used on Steam. The market is another sinister ploy from Valve, things might be worth a lot (cs2 skins for example) and people spend 100s on items thinking it's an investment. However, you can only use your money on Steam. Yet another way Valve has made gamers ultra loyalist and hostile to other, more developer friendly, platforms.