Can you provide a concrete example? I can't recall ever seeing such a disparity in the US having lived in the midwest, northwest, and east coast. Perhaps you're in a different location?
The Burlingame A&A is notorious for being the one of the cheapest -- if not the cheapest -- places to fill up between SF and SV. In this case, it's likely that the Chevron literally across the street just decided it's not worth playing that game, hoping instead to appeal to the crowd who wonders if the cheap competitor is actually up to par.
The fact that the Chevron gets any business at all seems to validate this strategy at first blush.
Also the Chevron at grant and el Camino is $3 when the shell a block away on grant is $3.60 or so. The Chevron is one of the highest trafficked gas stations in SV and operates on volume. The shell can't play that game so charges a pronoun to grocery shoppers.
Costco is 20% cheaper than all other stations in Toronto. So either Costco is selling at a loss (unlikely) or there's plenty of profit in gas for gas stations.
Given that you have to pay an annual membership fee to get access to that gas, it's likely subsidized by the fee and used as a loss-leader to bring people to shop at Costco.
It's an older link, but they still do much the same. There have been court cases brought against them by consortiums of other gas stations here and there, but AFAIK Costco has emerged victorious so far.