The story is set in an alternate-history late-Edo period, where humanity is attacked by aliens called "Amanto" (天人, "Sky People"). Edo Japan's samurai fight to defend Earth, but the shōgun cowardly surrenders when he realizes the aliens' power. He agrees to an unequal contract with the aliens, placing a ban on carrying swords in public and allowing the invaders to enter the country. The samurai's swords are confiscated and the Tokugawa bakufu (shogunate) becomes a puppet government.
The series focuses on an eccentric samurai, Gintoki Sakata who works as an odd-jobs freelancer. He helps a teenager named Shinpachi Shimura save his sister Tae from an alien group who want to send her to a brothel. Impressed by Gintoki, Shinpachi becomes his freelance apprentice to pay the bills and learn more about the enigmatic samurai. When the pair rescues a teenage alien girl with super strength, Kagura, from a Yakuza group, they accept her into their freelancing business and the three become known as "Yorozuya" (万事屋, "We do everything" or literally "The Anything Store").
While working, they regularly encounter the Shinsengumi police force, who often ally with Gintoki when work involves dangerous criminals. The trio also meets Gintoki's former comrades from the Amanto invasion, including the revolutionary Kotaro Katsura who is friendly toward them despite his terrorist activities against the alien-controlled government.
Although the story is mostly episodic, a few story arcs and recurring antagonists develop. For example, Gintoki's former comrade Shinsuke Takasugi is a major antagonist who regards Gintoki and his other former comrades as enemies and seeks to destroy the shogunate. Over time, Takasugi gains allies, including Kagura's brother Kamui, and the elite fighting unit Mimawarigumi to prepare for his large scale coup d'état. After the true antagonist—the immortal Utsuro—is introduced, Gintoki works with both friends and enemies to stop Utsuro from destroying the Universe.