It is the same when you use these prepositions for locations.
"The rule" is kind of vague and has lots of exceptions, but in general it is as follows:
- "в" for "closed" spaces like buildings. "Вокзал" is, surprisingly, used with "на"
- "на" for "open" spaces like a field or crossroads. "Аэропорт" is, surprisingly, used with "в", and so is "район" ("district" or "area")
- в for cities, countries, villages and other settlements. Exceptions: in Russian it is still more preferable to say "на Украине" (has lots to do with the etymology)
- на for rivers, islands. Yup, Япония is used with "в" and Хоккайдо with "на".
- the main tweak: use "НА" when referring to an abstract event rather than a physical space :"на концерт", "на занятия", "на работу". However, "в" is used for "кино" or "театр"- as I have already said, the rule only works on average for ambiguous cases

- ... and use "к" with dative when going to a person ("I'm off to Mary's place").


Now, for places you use it like that:
у Маши
в школе
на концерте

For directions (to corresponding objects) you automatically have:
к Маше
в школу
на концерт

And when going back:
от Маши ("away from Masha")
из школы ("out of the school")
с концерта ("from the concert")

I.e. you need to only know one of these cases. For example, if you are sure you use "из магазина" for "from the store", then you know how to say "to the store" and "at the store".