I think this famous Marx quote first appears in the introduction of Contribution to the Critique of Hegel's Philosophy of Right, and I think it is famous from being in the Communist Manifesto, correct me if I am wrong here?
The form of the quote in "The Critique" is "It (religion) is the opium of the people". This quote is often cited, and often taken at face value to mean that religion is not for enlightened person, but rather something indulged in by the ignorant. Those who accept this quote at face value view religion as just something the ruling class keeps in place to pacify the exploited in an anti-revolutionary manner.
Marx's meaning is actually far deeper and more relevant. The quote in "Critique" is in the context of religion being dialectically both an "expression of real suffering, and a protest against real suffering". Without real suffering even the (ignorant or not) masses would feel no need to turn to religion. Religion is not always imposed by an aristocratic ruling class (as in pre-bourgeois revolutionary France), sometimes it is used by the oppressed as a means to get through the day. For a good example of this look up "Liberation Theology"
I read Marx's point to be that there is real suffering in the world, and we need to remove religion to begin to address it (this is contrary of course to Liberation Theology, which is part of the reason I think they are no better than social-democrats). When suffering is removed in the mind from the realm of the real into the realm of the supra-real it becomes impossible to address in the real. If I am poor and jobless because God is angry then the solution is to sacrifice my favorite sheep (or Lily). On the otherhand if I am poor and jobless because the demographically tiny capitalist ruling class has crashed our economy, evaporating the jobs of millions of proletarians (and however you want to define service workers) then the solution may be to remove the capitalists.
It goes without saying then that religion and socialism are incompatible. Many have argued that socialists are "soulless", and that it is impossible to be both socialist and believe in a higher power. I think socialism is incompatible with religion, but does not have to be with spirituality or belief in a higher power. I myself lean towards atheism in terms of a god who takes an active interest in our lives, but if Stephen Hawking doesn't know what happened before time started at the big bang then I sure as hell don't. So that being said it is possible to be spiritual and socialist, just not religious and socialist.
As Marx himself said (also in the Contribution to the Critique of Hegel's Philosophy of Right "It is the task of history, therefore, once the other-world of truth has vanished, to establish the truth of this world." ie. it is our task as Marxist historians to pull back the veil of religion and establish the truth of the material world, and thus capitalist exploitation. (Yes Ian I know that was a very "Orthodox" statement to make.
Religion is the opiate of the masses, not that it makes them feel warm and fuzzy and forget their pain, but rather that it fogs their minds and causes them to misplace their suffering onto something out of their control. The true path to the end of misery for the human race is neither god nor the end of exploitation, but one leads nowhere and the other is a step in the right direction.