In brief, it's to take something that typically causes a strong negative result and accentuating the small positive effect that the substance (or event) has in order to produce a substantial gain.
In this example, opium induces a dream-like state that enticed some creative authors to smoke it. Its effects are completely sedative to the point that slight overdoses kill. Because people who use opiates (Morphine and Heroin included) are out of the context of reason, it can quickly become a bad thing. This unattached state is often referred to as being "stoned".
For someone to turn this use to gain means that the dream-like state it induces can excite the mind in a psychotropic sense, and that imagination could then be used to write a creative story out of the context of reality. Other authors, such as Mary Shelley (Frankenstein) and Charles Dickens (A Christmas Carol), would also take this highly addictive drug to induce these dream-like states and use that experience to sketch out characters or events in their stories.
Using the slight positive aspect of something with such a strong negative effect is what "turned its use to gain" is referring to.