Three main ways and it depends on the state of development in the country.
The first is the obvious option. Buying land with cash and paying to build a house. This might sound insane to many people but land doesn’t have the same value everywhere, and more specifically not all places have the same income to cost of land/development ratio. Basically what this means is that in some countries, being middle class and saving up for 20-30 years can leave you with enough money to be able to build a house out of pocket. It can even happen sooner if someone makes above average money, or their spouse makes decent money too, or multiple family members chip in. In Europe this was very common during post war development.
The second way is through a Ground Lease agreement with developers (I might have the term wrong). These were/are popular in areas/countries that do not have an economy that can easily sustain massive development firms and where most land is owned by people rather than companies. As such developers are much smaller scale companies which prefer to employ this method instead of buying land and then developing it. Basically the developer approaches the owner of a plot of land and offers to build on it. In return the owner retains a percentage of ownership of the building and the developer gets to sell or rent the rest of it. This is most commonly used for apartment buildings where the land owner gets to keep some apartments and the developers can sell or rent the rest. Usually this land is left over in families from their grandparents or earlier, from times when it used to be usable farmland but has eventually been built around and enveloped by urbanisation, meaning it can’t be farmed any more but the family may lack the capital to develop it.
The last option derives mainly from the conditions that enable the other two methods. Inheritance. Just as people may inherit an empty plot, they might also inherit an apartment, or multiple apartments, or a whole detached house. If land is mainly owned by individuals and not companies, every house you see is owned by someone, and eventually that someone will leave it to someone else. As long as the owners don’t get themselves in massive debt, it’s safe to assume the house will remain with the family for many years.
Latest Answers