Did my insurance company scam me when they paid less for my car than I saw it selling for at a used car dealership later?
No.
When a vehicle is damaged in a manner covered by its insurance policy, the very first thing that happens is that the insurance company assesses the vehicle to determine whether repairing and returning it to you is going to be worth it to them, based on your policy's value.
If the answer is no, the insurer deems the car as "totalled" and auctions it off for whatever they can get for it. "Totalled" therefore does not mean the car is undriveable or only fit for scrap, it's merely a value judgement made by the insurer.
What happened in your case - and what happens in many cases - is that someone viewing the auction for your former vehicle saw it, determined it could probably be repaired, and bought it. They were then indeed able to repair it, after which they sold it to the used car dealership.
It's important to understand that the person who bought your totalled car was taking a calculated risk. If they're correct, and the repairs are relatively minor, they will make money by repairing it and selling it on - as seems to have happened in your case.
But - and here's the kicker - if they guessed wrong, they will lose money on your vehicle. And there's nothing protecting them in the case that happens. Since the only thing the buyer has to go on when viewing the car at auction is a short description and images (since most vehicle auctions are conducted online nowadays), there is always a chance that there is something catastrophically wrong with the vehicle that they simply won't know until they get it into their shop and tear it down. A good example is the vehicle's frame - it's the most vital part and as such damage to it is often fatal, but it's also the most difficult to properly inspect.
Additionally, being deemed totalled has significant implications for a vehicle in terms of selling it on. A totalled vehicle is technically supposed to end up in a scrapyard, so if someone does end up repairing it, the vehicle actually has to be sold differently (salvage title) - prospective buyers are required to be informed that it was totalled, and as such there may be (potentially dangerous) defects that the repairers didn't discover.
For the insurer, that potential for undiscovered dangerous defects is a massive risk. If they repair your vehicle and return it to you, and you end up in an accident where it can be proven that an issue from the previous accident and/or the repairs conducted for said accident caused you injury or death, the insurer will be on the hook for a potentially crippling lawsuit.
At the end of the day, insurers are risk-averse. That's why they don't do a particularly thorough assessment of the vehicles they deal with, certainly not to the point of stripping the vehicle down. Their assessment is very much a "is this going to be quick and cheap and safe to repair?", and if not the vehicle is going to end up as totalled.
Does this mean insurers end up marking many perfectly repairable vehicles as totalled? Yes, and that hurts their bank balance. But since every one of their policies has a certain amount of fat built in to cover the scenario of totalling the covered vehicle, and the number of people who have insurance but never claim is large enough, it makes more monetary sense for the insurer to total "good" cars than it does for them to risk trying to repair them.