What do "sama" and "kun" mean? Are they related to the shinobi rank?
Both terms are Japanese honorifics.

Sama (様) is the formal version of "san", used for persons in higher positions (inferiors to superiors). On the other hand, kun (君) is informal and mostly used for males, such as boys or juniors. It is used by superiors to inferiors, by males of the same age and status to each other. In schools, teachers address male students as "kun" while girls as "san" or "chan".

As for anime Naruto, Yamato called Kakashi as Kakashi-sama simply because of respect for the higher position. Kakashi is a lot experienced than him. Meanwhile, Hinata called Naruto with "kun" as an informal honorific for a boy with the same age, that applies too for kun.