The first is the very high cost ($ billions) of getting through FDA approval for a drug. Once a company has done this approval, then other companies must show drug equivalence in order to produce generics that can stand in. They have to wait for the patents to expire, or figure out how to get around existing patents, but that's still not enough. They must show that their drug is equivalent to the existing drug. But this is not easy task, because the incumbent will do everything they can to prevent anyone else from getting the drug for research purposes. (e.g. the incumbent will claim that a particular company trying to get the drug does not fit their safety protocols, and thus refuse to provide the drug. Because the company cannot get access to the drug, they can't prove equivalence.)
Another method is that a prescription for one drug does not always guarantee that the prescription can be used for a generic drug of the same sort [0]. The incumbent company will spend lots on marketing by buying lunch for doctors, giving them notepads and pens, etc. Then, the doctor will write a prescription for that drug as write "Dispense as Written." This prevents the pharmacist from providing a generic alternative, even if they're biologically equivalent!
A final method is to develop a drug with a particular injection mechanism. For instance, if a drug company puts MDMA in a patch, then they can get a patent for the combination MDMA + patch, get approval, and then get doctors to prescribe it. (Maybe it provides a small dose over a long period?) This can occur even if MDMA was first synthesized a century ago. The prescription for the patch cannot be used for a pill form, or vice versa. This guarantees the drug company revenue as long as they can convince doctors to prescribe it.
So, to make a long story short, there are a lot of ways that drug companies guarantee their $$$ and prevent competitors from eating their lunch.
[0] https://www.washingtonpost.com/national/some-doctors-insist-...