The beginning of Gintama clearly mentions various times the sword ban but the Shinsengumi are constantly carrying around swords. Why is that allowed?
Gintama's setting is mix of fictional inter-galactic universe (which might be depicting foreigner coming to Japan during that era) referencing to actual Japan historical era specifically Bakumatsu period.
In 1876, samurai were banned from carrying swords. A standing army was created, as was a police force. This "sword hunt" was performed for, ostensibly, different reasons, and certainly with different methods than those of several centuries earlier. Ironically, perhaps, this sword hunt put an end to the class system while the earlier ones were intended to deepen the distinctions between commoners and nobles. Ultimately, however, the result of this sword hunt was the same as the results of its predecessors; the hunt ensured that the only weapons were in the hands of the ruling government and not available to potential dissenters. https://en.wikipedia.org/wiki/Sword_hunt
During this time Shinsengumi is the special police force that are there to protect the government. So it's just the same as now where the military and police may carry gun but commoners will need certificate to be able to carry gun legally.