Theoretically the role they serve is that they can negotiate with pharmacies and develop a formulary which insurers package into their various offerings. PBMs can negotiate with pharmacies by sending them lots of customers in exchange for negotiating for a discount (or, more likely, a rebate) on their "usual and customary" price. (Pharmacies know they do this, and thus they charge very prices to the uninsured, to ensure their U&C is high enough that they can still make a profit after applying the PBM discounts). Insurers are not experts in the local pharmacy markets of particular geographies, so in essence they outsource this negotiation and craft plans with formularies prepared by PBMs.

GoodRX and other discount providers generally work in one of two ways:

1) They have relationships with multiple PBMs, allowing you to choose the one who has negotiated the cheapest rate with the pharmacy for the drug in question. This is why it might be cheaper than your insurance: another PBM has negotiated a better deal.

2) The discounts come from patient assistance programs run by the manufacturers intended to reduce patient co-pays. Lately insurance companies have started to add clauses to their plans (called copay accumulators or copay maximizers) so that these discounts don't count as part of your copay or your deductible. So these types of discounts are going to be harder to get.

This all stems from a time when pharmacies were much less consolidated and vertically integrated than they are today.

One of the frustrations of the current system is that incentivizes sky-high drug prices. PBMs like high drug prices because they negotiate rebates (some of which they keep, but most they pay back to the insurer) and because the fees they charge to insurers are a percentage of the claims that go through. Pharmacies like high drug prices because they get more money paid them in reimbursements, and because the PBMs send them most of their customers. Manufacturers like high drug prices because they net more revenue, even if they later have to pay it back in the form of rebates, and in any case being on the formulary of major insurers is an existential issue for them. And insurers like high drug prices because they can max out patient co-pays, as the money returned to them in the form of rebates gets kicked into the general fund, thus allowing them to lower premiums, which is their primary axis of competition with other insurers.

The net effect is that you have sick people maxing out their deductibles in order to lower the premiums paid by healthy people--the exact opposite of how insurance is supposed to work. If I could wave a magic wand in Congress and make only a single surgical change to healthcare markets, the change I would make is banning rebates. They were anti-customer when John D Rockefeller used them to obtain a monopoly on oil, and they are anti-customer today.

A good place to read about these dynamics in American healthcare is drugchannels.net. The author is super well informed on how these plans are implemented.

Source: ran a startup targeting pharmacies (which failed) and currently work in a starup focused on discovering and developing new drugs.