There is always risk when buying a used vehicle. There can be title problems, undisclosed prior collision history that does not show up on CARFAX, or mechanical problems.
Franchised dealers are the lowest risk but have the least competitive pricing. CPO gets you an inspection that will generally only find obvious problem and an extension of the manufacturer's warranty. CPO vehicles can still be losers and have tons of mechanical problems. Franchised dealers will usually pull CARFAX, but CARFAX does not pick up everything and it can take months for damage reports to make it onto a CARFAX report. So, you can buy a nice looking vehicle with a clean CARFAX and find out three months later that CARFAX just began reporting negative info. Dealers do have to do a safety and emissions inspection in most states which will mandate that safety equipment (lights, brakes, etc.), tires and the emissions system are all working.
Independent dealers (used car only lots) come in all different shapes and sizes. CARMAX and some regional large used vehicle dealers will be on par with franchised dealers. Smaller independent dealers will just get a vehicle at auction, put it on the lot and tell you to f#ck off if there are any problems.
You can get deals buying vehicles outside of the licensed dealers. But I would only buy from friends or family. There are way too many stolen vehicles being sold on Craigslist and a lot of curbstoning by licensed dealers to off load junk from their lots. If you buy from a thief, you get no legal title and the police can seize the vehicle and give it back to the rightful owner, leaving you to try to recover the money from the thief (who is usually working to launder money for organized crime/drug cartels). You generally cannot get an auto loan to buy from a private seller because the lenders do not trust them to transfer good title and perfect their lien.
You can get a vehicle inspected by your mechanic whether you are buying from a private seller or licensed dealer. But a mechanic can only find obvious problems and will not be able to discover latent defects. Unless the vehicle is throwing a fault code, has an obvious performance issue that can be consistently replicated or has a clear visual issue (fluids leaking, etc.), a mechanic will not be able to tell you whether a vehicle will be problem free 6, 12, or 24 months from the date of sale. It takes about a day for a mechanic to tear down an engine and inspect the internal engine (cylinder heads, crankshafft, etc.) and costs $2,000+ depending on the make and model of vehicle.
The safest route is to buy from a franchised dealer and to buy a vehicle that the franchised dealer is licensed to sell (used Toyota from Toyota dealer, etc). Ask the dealer for a copy of the auction receipt or the used vehicle appraisal report and reconditioning repair order if the vehicle was traded. The big auctions require damage disclosure by sellers and will have some notation of the same on the auction receipt. Most dealers will write up an appraisal report on trade vehicles noting any damage or mechanical issue and will have a repair order from the service department detailing any repairs done to recondition the vehicle. You can also ask them to pull the service history for the vehicle. Most franchised dealers will be able to access service history from all the franchised dealers where the vehicle was presented for service.