This is a genuinely important thing to get right, and it applies to everyone, not just women, though the practical guidance differs slightly.
Within resort grounds and on private beach areas attached to hotels, normal beachwear, including bikinis and swim shorts, is entirely fine and expected. This is the one context where standard beach dress is completely appropriate.
The moment you step off resort property, into Stone Town, local villages, markets, or anywhere with a genuine local community around you, dress more conservatively. For women, this means covering shoulders and knees as a sensible baseline, loose trousers or a longer skirt with a t-shirt or shirt work well and stay cool in the heat. Swimwear should never be worn walking through villages or Stone Town, even just covered by a light shirt over a bikini top.
For men, this means wearing a shirt rather than going bare-chested away from the beach itself, and knee-length shorts or trousers rather than very short shorts in town settings. It is a smaller adjustment than for women but still noticed and appreciated.
Stone Town in particular, with its mosques and more traditional atmosphere, warrants extra care, and if you plan to visit a mosque, modest dress covering arms and legs is required, with a headscarf for women.
This is about genuine cultural respect rather than a strict rule enforced on visitors, and Zanzibaris are generally warm and welcoming regardless. Dressing thoughtfully outside resort areas is simply appreciated and tends to lead to friendlier, more genuine interactions with local people.