This is probably very oversimplified but...
Imagine buying a $100 drug at 2 pennies. Possible for your patient bucket (say you are a hospital that uses 340B) that qualifies. For each patient in that bucket that uses this $100 drug you spend only 2 cents per patient. Now let's say that person has an insurance that covers the drug. Many insurances reimburse pharmacies at a rate of AWP - % plus a dispensing fee which is unrelated to the 340B acquisition cost. So you bought the $100 for pennies but then got reimbursed as if you had brought it at the "standard wholesale price."
Now apply that example to specialty meds like hep C that can cost $1000 per pill or more. Hopefully that "paints a picture" how profits can be made
FYI that's just my understanding of the basics. As I said it's much more complicated than that and I just graduated so I haven't really seen a lot of it in actual practice. Lol