Contracts are negotiable, so terms can be flexible. For instance, a buyer might hire an agent to represent him only on homes shown on a given day. Others charge no upfront fees. Most buyer's agents, like seller's agents, work on commission. But the seller, not the buyer, typically pays.
Sellers usually agree to pay because they consider commission a cost of selling their home.
They pay commission to their listing, or selling agent, who in turn splits it with an agent who finds a buyer. In some cases, that co-operating agent works for another real estate company but still works in the interest of the seller. In other cases, the co-operating agent is a buyer's agent. They might allow for that expense in the sale price of their home. If a homeowner refuses to pay the commission, the buyer would have to pay.