The samurai were part of the social class known as the "Bushi". The Bushi class was a social class for the warriors and samurai of Japan at the time. This class that the samurai were actively a part of ranked below nobles but above commoners and peasants. While in this class, samurai served their Daimyo with protective services in return for land and held noteworthy authority and influence. For the entirety of the samurai's time in the Bushi class, they were always bound by the samurai code known as the "Bushido." As times in Japan got safer and peace was established by the Tokugawa Shogunate, the samurai lost their jobs and a significant amount of power. The now-out-of-a-job samurai looked for jobs as they were now deemed unnecessary and mostly switched to bureaucrat jobs.
The word Bushido translates to "The Way of the Warrior" and was the code for hundreds of years that all Japanese samurai had to follow. The Way of the Warrior code stated how samurai must pledge their allegiance to Japan before anything else, including their children, their wives, and more surprisingly themselves. This code pushed samurai to exhaustion and set limitations to life as a Japanese Warrior. Infamously this code is known for the ritual that aligns with it, seppuku. Since samurai would rather die than wrongfully serve their country according to the Bushido, samurai had to kill themselves in a process known as seppuku when close to defeat. Samurai only did so when they felt they had disobeyed or were near defeat from an enemy in battle. They did this to show their loyalty and to honor the nation they had served. Lots of the Bushido regulations and rules derived from Buddhism and Confucian ideology, which were the common religions amongst samurai.
Women samurai did exist but were not as common since women were traditionally supposed to learn the arts of poetry, literature, music, etc. They weren't necessarily samurai, rather they practiced martial arts with occasional sword use. "These women were referred to as the onna bugeisha" (Cartwright). Some famous samurai women were Nakano Takeko who fought very honorably killing many men, the 20 women of the Aizu clan who rushed and defended front lines of many battles, and the women of Kagoshima who fought in the Satsemo Rebellion of 1857 fighting the imperial army.
In the Edo period Confucianism returned in a time much needed to save the samurai. Merchants were flourishing in the Japanese economy along with craftsmen and artisans while the samurai struggled to profit during the era of peace. Samurai constantly had advantage in power and wealth, so no reforms were necessary. Confucian morals believe that merchants should be at the bottom of society and viewed as parasites. This allowed samurai to reign supreme, and the Tokugawa Shogunate further buffed them allowing to kill anyone who seemed rude. Samurai still were paid by daimyo for being a standby guard even in this absense of battle. "Samurai began to receive discredit in their societal superiority, as they would commonly cut down peasants to see if their swords were sharp enough in the process called tsujigiri meaning cutting down at crossroads" (Cartwright).
During the Edo period samurai ranks were developed. There were the gokenin (housemen) who were the lowest vassals of their daimyo, the goshi (rustic warriors) who could farm their land but not have the two swords that showed samurai rank, and the hatamoto (banner men) who were the highest ranked and the only ones who were to die to protect their lords. The name banner men is derived from the tradition of samurai charging into battle with banners of their family heritage with them. The hatamoto were forbidden from farming land (samurai would farm and live on their land when not battling) in order to further the loyalty to their lord. The two sword policy was a major law passed by Toyotomi Hideyoshi, one of the three unifiers, that declared only the experienced/future hatamoto samurai could hold two swords (the traditional long curved and short sword). This policy was needed both for clear identity but there was a time where even skilled peasants could possess swords and use them. Before Hideyoshi and this policy there was a National Board Of Retainers that would monitor and distribute the lower class samurai (future gokenin) to serve the shogun and/or daimyos.