This quote appears in Marx's 1859 work A Contribution to the Critique of Political Economy.
What Marx means by this statement, which is central to his philosophical construct, is that economics, not abstract ideas, are the basis of how we perceive the world. Economics comes first. For Marx, economics means...
our way of surviving as human animals: how we get food, clothing, and shelter. We must have these items to survive. Therefore, they are inevitably the primary concerns that dictate how we organize society—and hence the way we think.
For example, our values are determined, Marx argues, by how we obtain the means of survival. If we are dependent on hunting to stay alive, we will form a consciousness—a group way of thought—that puts the highest moral value on the attributes of good hunters, such as physical strength. If we live in a capitalist society, we are not likely to place physical strength in the highest regard (though we might value it) but to valorize such attributes as those that make a Bill Gates or Elon Musk: intelligence and pragmatism. As is parodied in The Simpsons, a person in our society can be physically weak and still command respect if he owns a profitable nuclear power plant, because money is the route to physical survival in our culture.
From this economic basis, Marx argues that our consciousness is formed by our social class, and our class is determined by how we earn a living (survive physically). How we think and what our values are are determined not by some abstract ideal, but by the need for survival. For example, the Ancient Greeks didn't just sit down and decide in a vacuum to valorize warrior heroes—they valorized those people because of their role in generating wealth.
Class consciousness inevitably causes class conflict, as each class fights for its share of society's resources. This leads, as well, to the upper classes distorting people's consciousness (called mystification, as in creating a mist, or false consciousness) in order to justify holding on to disproportionate wealth. For this reason, the working class in the nineteenth century, Marx said, had been deceived into thinking that capital (money itself) was the basis of wealth, but would soon realize it was their labor power, at which point they would rise up and take power.