Metaphor
Marx used the phrase to make a structural-functionalist argument about religion, and particularly about organized religion.
In his view, religion may be false, but it is a function of something real. Specifically, Marx believed that religion had certain practical functions in society that were similar to the function of opium in a sick or injured person: it reduced people's immediate suffering and provided them with pleasant illusions which gave them the strength to carry on. In this sense, while Marx may have no sympathy for religion itself, he has deep sympathy for those proletariat who put their trust in it.
However, Marx also saw religion as harmful to revolutionary goals: by focusing on the eternal rather than the temporal, religion turns the attention of the oppressed away from the exploitation and class structure that encompasses their everyday lives. In the process, religion helps to foster a kind of false consciousness that emboldens cultural values and beliefs that support and validate the continued dominance of the ruling class. It thereby prevents the socialist revolution, the overthrowing of capitalism, and the establishment of a classless, socialist society.
In Marx’s view, once workers finally overthrow capitalism, unequal social relations will no longer need legitimating and people’s alienation will dissolve, along with any need for religion.