A bathroom fan only does its job if it vents the moist air all the way outside. Venting it into the attic — a common mistake — just dumps the moisture where it causes hidden mould and rot.
Always vent outside
Run the fan's duct to a dedicated exterior vent — through the roof with a roof cap, or out a wall or soffit with a proper hood. Never terminate the duct in the attic, a wall cavity, or near a soffit intake where the moisture gets pulled back in.
Duct it well
Use smooth rigid or semi-rigid duct rather than long, kinked flexible duct, keep the run as short and straight as possible, and slope it so condensation drains outward. Insulate duct that runs through cold attic space so moist air doesn't condense inside it.
Seal and size it
Seal duct joints with foil tape, and make sure the fan is sized for the bathroom (enough CFM). A timer or humidity-sensing switch ensures it runs long enough to clear the moisture after a shower.