When I am queen of the world, carpeting in bathrooms will be outlawed, under penalty of death. Ew.
Anyway, chances are years of high humidity have trapped all sorts of moisture, leading to mustiness, but hopefully not mold. Since you’re renting, we’ll try to keep the solutions cheap and easy (although, depending on your relationship with your landlord, it may be worth asking if you can rip it up if you’re planning to be there for a while). Ventilation and air flow is pretty key here. If the bathroom doesn’t have an exhaust fan, you should be running a freestanding fan as much as possible. You can try covering the carpet with a fairly substantial layer of baking soda, letting it sit, then vacuuming it up.
Truth time, though: carpets in bathrooms are a BAD idea, and because they’re constantly damp, the musty smell is next to impossible to get rid of. Pester your landlord if you’re planning on staying there for more than a year.