There are a couple of different methods used. I know chromatography is one of the more common ones. If you read through the top comments they highlight the biggest current problems, which is poor/lack of standards. It's not just the labs, but the sample selection process and chain of custody. Currently producers are allowed to cherry pick samples from a batch and there is no sample-batch chain of custody process to prove that the samples were randomly taken from the batch, and the batch remained untampered with from time of testing to time of retail sale. People have proven this by selecting particular grades from a batch and sending them in for independent testing(i.e. choosing the best looking, worst looking, and then average). Results varied as much as 40% for the same batch.
Then there is the problem of these advanced HPLC machines that use complex software and need to be calibrated to a high degree of accuracy. Reference standards is also a reason for fluctuations from the same batch sample between different labs. Then there is room for human error in the lab technicians as well. Just recently someone got called out for having distillate that tested at less than 3% thc at an independent lab. Later the lab called them back and said that the 2% was a mistake- it was only ~2% d9thc, but it showed a mystery isomer hitting ~90%- likely another isomer of thc like d8 or d10. These labs are also often inundated and charge very little making it cost prohibitive to double check.
There's also the problem with "undectables" with all lab screening. Most methods will only detect the presence or absence of things they are testing for. So if they are not testing for it then they likely won't know it is there. It's one of the ways athletes can use steroids and still pass a drug test- they find steroids that they aren't currently testing for. The same way people can pass a drug test while using synthetic cannabinoids, producers can be using plant growth regulators(pgr's), herbicide, pesticides, and other chemicals that can be harmful and still pass a test. Not all producers do, but there are certainly some. Connected is currently the subject of a lot of controversy for supposedly using chlormequat, a pgr not tested for.
Other countries like Isreal are far more advanced in this area. Accurate testing is certainly possible but is currently a ways off. Legalization/recreation has been a step forward compared to medical, but reports of bad practice, malpractice and even corruption has been reported in every state.
There is definitely clean product out there and brands that honor their word and take pride in honest work, but there are definitely a lot of sharks, uneducated, unexperienced, greedy, and people who don't care. The market attracts all kinds but in particular, people looking to get rich often by any means. Much different than any other kind of farming.
Even with federal oversight like the FDA it's still good to have a healthy amount of doubt. The recent lawsuit against Monsanto for their product roundup(glysophate) being proven to cause cancer. There is only so much we can do, I know money is a big obstacle for most. I just think when it comes to something we are inhaling we should be a little more concerned. Side effects from smoking PGRs are pretty nasty.