they were the same people a lot of the time, people think of samurai like these super honorable lawful almost religious guys but that was made up after samurai were irrelevant (kinda like europeans and knights), back in the day, if you weren't willing to cheat, lie, steal, murder, etc. for your Daimyo, you weren't really loyal at all - many 'ninja' were simply samurai or their men concealing their identity, or were otherwise specialist mercenaries hired by samurai/feudal lords in secret. in some eras and places Samurai would even test new blades by hiding near a road and sneaking up and killing peasants at night, a good sword was supposed to kill in 1 clean cut - sounds very much like Ninja behavior to me.
they were basically the same as european knights, bloodthirsty warlords working for the local barons, and they would absolutely use stealth or subterfuge to accomplish their lord's will, because they were more or less intelligent rational people trying to accomplish material goals, not cartoon zealot idealists obsessed with 'honor' and 'purity' - the only 'honor' is serving your lord, the only 'purity' is in unwavering loyalty.
If I remember right, originally ninjas had like their own specific clan that rich people from other clans would hire to basically do a hit on their competitors/enemies/rivals. Taking money from rich people to kill other rich people ain’t too bad of work
Historical ninja kind of spanned the class spectrum, as I understand it. Some were samurai, some were mercenary clans that had regular dealings with samurai, and some were people who lived in autonomous mountain enclaves that were too remote and defensible by the locals for the lords to claim them at the time
Also, the word 'ninja' wasn't contemporary, it was appended after the fact. The common thread with the people and groups who received the label was that they were believed to have used relatively covert and trickster-y methods. There may be an element of internalized orientalism and noble savage mythmaking in the mix, as many popular conventions of the ninja archetype were codified in a pop-culture ninja boom in the 1960s, directly in the wake of the US occupation.