The idea is a transitioary dictatorship with the goal being to eventually dismantle itself. It's there to prevent capitalist groups taking advantage of the power vacuum caused by a revolution & making everything worse.
It's been a while since I've read up on Marxism but IIRC this is the classical theory of how we'd get there, but there are also more modern ones. One theory is that communism is inevitable as technology progresses, because as we get closer to post-scarcity, workers have less negotiating power & the pressure will either push workers to overthrow capitalists & redistribute wealth. Or, alternatively capitalists will willingly redistribute the wealth they get from automation to avoid being beheaded, or, out of benevolence.
IMO Marx was incredibly ahead of his time and just saw all the flaws in capitalism and wanted to find solutions, but the reason communism "doesn't work" isn't that the methods in trying to get there are flawed. We simply aren't ready for it. It relies on so much trust and people are too individualistic for it to work on a large scale until that changes. I help run a worker's co-op where shares are distributed equally based on how many hours you work and since it's remote & synchronous it runs entirely on trust. It works, but we have to be very selective about who we let join. You can't do that with countries.
IMO there's no right or wrong economic system, these systems are reflections of where our collective beliefs are. If everyone wanted to share & treat each other equally, communism would happen overnight by accident. Capitalism is flawed because it's the result of people being flawed.