late 14c., drogge (early 14c. in Anglo-French), "any substance used in the composition or preparation of medicines," from Old French droge "supply, stock, provision" (14c.), which is of unknown origin. Perhaps it is from Middle Dutch or Middle Low German droge-vate "dry barrels," or droge waere, literally "dry wares" (but specifically drugs and spices), with first element mistaken as indicating the contents, or because medicines mostly consisted of dried herbs.
Compare dry goods(1708), so called because they were measured out in dry (not liquid) measure, and Latin species, in Late Latin "wares," then specialized to "spices" (French épice, English spice). The same source produced Italian and Spanish droga, Swedish drog.
Specific application to "narcotics and opiates" is by late 19c., though the association of the word with "poisons" is from 1500s. Druggie "drug addict" is recorded by 1968. Phrase a drug on (or in) the market "thing which has lost its value and is no longer wanted" (mid-17c.) is of doubtful connection and may be a different word, perhaps a play on drag, which was sometimes written drug c. 1240-1800.