If you buy someone's used iPhone

If you buy someone else's used iPhone, does the iPhone know that someone else once owned it and kinda sync your itunes account to theirs? Or anything like that haha?

The iPhone doesn't "know" anything.

When the other person sells it to you, the seller should do a factory reset. This will erase everything on the phone and be ready for you to set up and activate. It will not "know" that it was with someone else.

They should do a factory reset on it before selling it.wiping out all the accounts

As long as it has been properly reset ****and the icloud lock stage done to unlink it from the owners account**** it's no different to a new phone.

If it is still linked to an Apple account, it will irrevocably be locked for any of many reasons and be useless.
The only way past the icloud lock is for the account owner to enter their login info.
It is cross-checked with records on Apple's servers so nothing else done to the phone can ever change it.

When buying an Apple device you should always insist the seller does a full factory reset and gets it back through activation to the normal screen whilst you watch, to prove it is not icloud locked - and do not accept any exude they may have to avoid doing that, or go on the promise that they will give you the password later…

There are many stolen phones etc. About and with one of them it's effectively "bricked" during a factory reset.
(And "finding" a phone is still criminal theft in law, if they try to claim that).

Well no. The previous owner (or you (if they didn't do it)) is supposed to wipe the device of any log in info previously used.

First make sure it's not been reported stolen then you can reset it.