Only children under six and people with health issues are exempt from the law, which comes into force on Thursday.
Many European countries have now made wearing masks a requirement on public transport but the Spanish decree goes further.
Spain has seen one of the worst Covid-19 outbreaks in Europe but is now easing the lockdown gradually.
It already requires the wearing of masks on public transport and is now strengthening the rules across the population. Spain has reported almost 28,000 deaths and 232,000 infections since March but the rate of infection has declined.
Spain had imposed some of the toughest measures on the continent, including keeping children indoors for six weeks. Prime Minister Pedro Sánchez addressed parliament on Wednesday ahead of a vote on extending the state of alert for two more weeks.
The decree states: "Using masks will be compulsory on the street, in open spaces and any closed place of public use, when it is not possible to maintain a safe distance of at least two metres (6.5ft)."
While children under six are not required to wear masks, all between the ages of three and five are recommended to wear them. According to El País, that means 45 million people will now have to wear a mask and another 1.3 million will be urged to.
The law underlines that it is following World Health Organization recommendations to minimise infection in closed and public places where there is a large concentration of people.