Morocco is a country with a very particular culture and religion is important there, so you need to know some basic ways of behaving before you go there.
Morocco is a Muslim country, which, like many other countries in the Maghreb or elsewhere in the Muslim world, has tended to become more radical over the last few years or decades. For example, you see more and more women wearing veils in the towns, in the very traditional town of Tangier in the north of Morocco, in Casablanca, or in the capital, Rabat. It's a radicalisation which is opposed by part of Morocco's contemporary young people who dream of modernity, night clubs, electronic music and miniskirts.
The importance of Islam in the country and its radicalisation over recent decades means tourists have to be careful to respect Islam and always take it into consideration during a trip to Morocco.
In Morocco this respect applies to religious monuments, especially mosques, where you should be appropriately dressed and not showing too much flesh, but also to Muslim customs like prayer and Ramadan. Don't forget that during Ramadan it is forbidden to eat before sunset, so don't eat a cake in the middle of the street where everyone can see you.
In Morocco it is often said that you mustn't criticise the king, or religion or talk about the conflict with the Sahrawi people. These are 3 very sensitive subjects in this country and you should avoid talking about them in public during a trip to Morocco. The country's media is still muzzled and it's not possible to write or publish whatever you like, especially with regard to these three subjects.
The country is still quite traditional and doesn't look like getting any more modern with the passing years, sometimes quite the contrary. For example, abortion and homosexuality are still illegal in the country and punishable by prison.
The first and most important piece of advice is to dress in a way that respects the country's religion and traditions. In Morocco men shouldn't walk around the streets bare-chested, and nor should women wear mini-skirts, mini-shorts or plunging necklines. Most of the time on the beaches women go swimming fully dressed, and it's definitely a better idea in some areas than wearing a bikini. In general, on the public beaches wear a conservative swimming costume.
And never forget, especially when you are in very touristy areas or at the many and varied festivals, that you are still in a Muslim country and, even if you are surrounded by other foreigners, you should remember to respect their customs and traditions.
Learn a bit of basic Arabic so you can exchange a few words with the locals, which is always appreciated and may help you out of possibly embarrassing situations.