Opiates are some of the oldest known compounds to have been used both therapeutically and recreationally. This is likely in part due to their natural source - the opium poppy. This made it easy for societies to obtain this drug, especially in the Middle East where the poppy is frequently found growing.
It is likely due to the recreational uses that opiates have been used for such long periods of time, probably because of the psychoactive effects. In many countries, opiates are illegal to use recreationally, and are controlled substances for clinical use.