Aha! Something I know about!
First, a pharmacy is registered with the NCPDP (National Council for Prescription Drug Programs), and that gives it an identifier that is used to identify the pharmacy uniquely.
Then that pharmacy gets registered with a nationwide pharmacy network provider- the big one is SureScripts. Something like 95% of pharmacies are registered with them.
Once that’s done, a doctor’s office using software (usually an EHR/EMR) that interfaces with SureScripts (or a competitor) can choose a pharmacy in SureScripts and create an e-prescription to that pharmacy. Generally speaking, modern EHRs combine the actual e-prescription workflow with the recording of the prescription to save time, so the doctor literally e-prescribes it to you right then and there in a lot of cases as part of the visit.
Then, on the other end, the pharmacy’s software either checks SureScripts periodically or SureScripts pushes the prescriptions to the software (not exactly sure how that part works), and it’s there for the pharmacists to fill and/or review.
I used to work in healthcare IT; the company I worked for had a more convoluted version of the above, where in order to implement a clinical dispensing solution (i.e. doctors offices dispensing drugs), we basically had to register our clinics as pharmacies, and e-prescribe from our EHR to our clinical dispensing solution… in the same clinic.