[verb (plain)] + んです [i-adj] + んです [na-adj] + な + んです [noun] + な + んです
んです ties what you say to a situation both speakers can see: it gives the reason for something, or asks for one. English does the same with "the thing is", "you see", or just tone of voice. It follows the plain form, and a noun or a な-adjective takes な in front of it.
The past goes on the word before んです, not on です: いった行ったんです, さむかった寒かったんです, がくせい学生だったんです. Between a noun and んです the word is な, never だ.
A question with んですか asks about something you have noticed. どこに行きますか just asks where; どこに行くんですか asks because you can see the other person is going out.
Ending the background with んですが leads into a request, or lets you stop and leave the request unsaid.
A plain fact with nothing behind it does not take んです. Introducing yourself with it sounds as if you were explaining something nobody asked about, and a string of んです sentences sounds like a list of excuses.
These go beyond polite speech.
In plain speech the statement is んだ and the question is の?. Both are used by everyone; んだ? in a question sounds rough.
のです is the same form, written in full. It belongs to writing and formal speech; in conversation you hear んです.