Marx believed that in this kind of system, the laborers are oppressed. They work a lot and yet they get very little. On the contrary, the capitalist hardly works at all and yet they get almost everything. And it is in these circumstances that Marx made the aforementioned statements.
Due to oppression, people needed something to believe in. They feel weak and powerless against the system. They believe that there is nothing they can do. That change is beyond their power. And because of this desperation, the people needed to believe that there is a higher power. They need to believe, even if they don't understand why the system is so unfair, that all this is part of a greater plan. That someday, they will be at ease. They need to believe that someone out there is aware and sympathetic of their misery. That someone out there would give them comfort. Thru religion, the oppressed find hope.
As such, Marx believed that religion is a response to oppression. This is the reason why he said that religion is "The sentiment of a heartless world, and the soul of soulless conditions." Without a 'heartless world' or 'soulless condition', religion would not emerge. If people where truly satisfied with the current state of things, they would not look for other places for happiness. They would not turn to religion of hope and comfort.
In this context, Marx also said "Religion is the sigh of the oppressed cre ...