Time references with 後, 前, 先, and 時
後 is a noun that means "after". X後で is a conjunction saying that something happens after X, although not necessarily immediately.
昨日は夕食を食べたあとで、すぐに部屋に戻った。 Yesterday, after eating dinner, we went back to our rooms.
前 is the opposite of 後 and means "before". 前 can both mean before in the spatial sense (i.e. in front of you) and in the time sense (i.e. the past). This is unintuitive because of the notion of "putting the past behind you", but if you think about a line of people waiting, the ones in front of you will take their turn before you. So when it is your turn, theirs will have been in the past.
The conjunction X前に means something happens before X does, not necessarily immediately. If X is a verb, it cannot be in past form.
考える前に、体が動いていた。 My body moved before thinking.
前 and 先 are almost synonyms.
先 is a noun meaning "before", "ahead", or "front part/end". Generally, when 先 is spatial, it means the front ("tip") of something, or the direction it's headed, not the location immediately in front of it.
目線の先には、少女がいた。 A girl was before my eyes. (lit. “ahead of my line of sight”)
Just like 前, it can mean “before” in time too.
それは先に言った。 I said that before.
さっき is a casual version of 先 that is exclusively used to mean a "moment ago".
時 is a noun that means "time", but if you give it a relative clause, it can act like a conjunction (“when” or “at the time of”).
そんなことを考えている時、宿屋の入り口で大声が聞こえた。 As I was thinking about that, I heard a loud voice around the entrance of the inn.
Verb + 時 behaves differently depending on the tense of the verbs that come in the statement before and after it. There are various rules but in general if the verb before 時 is in past tense, it means that the action after 時 happens as the action before it has already been completed. Don’t try to memorize these rules though, just learn to recognize them as you go.
目が覚めた時、俺は知らない場所にいた。 When I woke up, I found myself in an unknown place.