well. a very simple explanation is that に is used for when action is towards something and for time. で(can also use へ) is used when something happens inside something/at something.
家に行きました。i went home.
家で宿題をしました。i did my homework at home.
however when something is inside something (or among), you use の なか で (の中で).
ペンはかばんの中であります。 the pen is inside a bag.
全部国の中で日本は一番好きです。from all the countries i love Japan the most.
(btw, i really hope there aren't any mistakes. even so - i am also learning. on the other hand it is summer break, haven't trained much, so it is forgiveable)