I learned that it's used for (1) explaining things, (2) seeking an explination, or (3) providing extra information/an extra comment on something that has just been said.
(1)
バスが来ませんでした。
The bus didn't come.
That's just an objective statement.
バスが来なかったんです。
The bus didn't come (so that's why I'm late).
It gives an explination of why you were late to whereever you were going.
Another example could be:
映画を見ました。
I saw a movie.
Objective statement.
映画を見たんです。
I saw a movie (and that's why I'm so happy).
It shows that you're explaining why you're happy.
(2)
どうしたんですか。
What happened? (You look awful!)
You're looking for an explination.
どうして悲しいんですか。
Why are you sad? (You look sad [lol])
Looking for an explination of why this person is sad.
(3)
A: このセーターが好きですよ。
B: 私も。祖母が編んだんです。
A: I like this sweater.
B: Me too. My grandma knit it (fyi).
Hope this helps