Gotcha actually has several meanings. All of them can be derived from the phrase of which this is a phonetic spelling, namely "[I have] got you".
Literally, from the sense of got = "caught, obtained", it means "I've caught you". As in, you were falling, and I caught you, or you were running, and I grabbed you.
It can also mean being caught to the red-handed Thus, gotcha is often used when you witness someone doing something naughty.
Gotcha can also mean Surprise! I tricked you! This sense of gotcha is used when someone falls for a practical joke.
Gotcha is also used for a situation where someone trips you up or catches you off-guard.
From the figurative sense of got = "understood", gotcha can be used to mean "Aha, I see now" or "I understand"