There are two main types of pensions to consider in answering your question:
(1) Defined Benefit pension plan. This type is the 'traditional' pension plan. It means that when you retire, you get a benefit based on, generally, years of service & salary over that period. Even if the market fails, the company still has a legal obligation to pay out the pension, and depending on jurisdiction, probably there is even some form of insurance / protection to try and keep the pension plan paying out, even if the company itself fails. The company likely has a legal obligation to keep the pension plan well-funded, meaning it can't just use new contributions from new employees to pay out retirees - if the market returns on the invested 'pot' are poor, the company will need to contribute additional money to allow for future amounts to be paid properly.
This type of plan works by having employees pay into the 'pension pot', and the company invests that money, plus additional money contributed by the company directly, into the market. If all goes well, the originally invested money will grow, and be able to pay for retirement. If the investments under-perform, the company will need to pay for any shortfall. This places the risk of market failure on the company, not the individual (except in case of complete uninsured bankruptcy, in which case the retirees might lose everything).
(2) Defined contribution pension plan. This type is becoming more common, because of the risks associated with the defined benefit plan. Here, the employee contributes money to their own personal 'pension pot', and the employer likely puts in some money as well. It is then up to the individual to choose how to invest that money [quite likely there is a limited number of options to choose from, decided by the company when they set up the plan].
Here, things work partly as you are guessing - the money invested earns interest, dividends, and capital returns, based on where the money is invested. However note that new employee contributions are for those new employees only - no one else pays for retiree payments once the company has put in the initial contributions. The risk of the market under-performing, is therefore placed on the individual; the company bears no risk of market failure.
There is a third type of pension plan, government run, which operates by having the whole population pay an additional tax, and pays out from that tax, to all retirees. This operates exactly as you are indicating - new workers effectively pay for the retirees to keep getting their pension. In the case where there are few new workers but many retirees, that can put a severe strain on taxing the smaller group of new workers.