Once upon a time, before the internet existed, the only way to distribute scientific content to a worldwide audience was through print. There are obvious costs related to printed publications such as paper, ink, printing, distribution, etc. Commercial publishing houses were established, which took care of this task, as well as the editing, the type-setting, organizing the review process (mind that the reviewers are typically not paid, but they still have to be found, contacted, etc, and all of that also costs money in a pre-internet era).
Many of these commercial scientific journals gained a certain reputation over time, and it became attractive for scientists to try to publish in the highest valued journals. For the publishers it was (and is) attractive to maintain the journal's reputation (e.g. expressed in its impact factor, etc), in order to attract an abundance of high quality manuscripts, select the best ones, and keep a large audience.
This was (and to a large extent still is) the status quo when the internet arose. This is also basically the answer to your question.
Now with the internet, it is perfectly possible to reach a large worldwide audience without the costs of printed journals (e.g. ArXiv). Also, peer review could be organized in an alternative manner. However, the commercial publishers have a lot of interest in keeping the old business model alive, as it is the source of their revenue.
So why are things largely as they were 50 years ago? There are a couple of reasons for this. For one, there is the absence of a platform that fully replaces the ring of scientific journals, including a reliable peer review process (or accepted alternative). Furthermore, people tend to do what they are used to doing in the past, and senior researchers (the ones who take the decisions) are used to publish in the traditional venues, and teach the juniors to do the same. Lastly, many researchers are (at least partially) evaluated with respect to the reputation of the venues they publish in, which keeps the old and established journals alive.