In そうなんですか (sounandesuka) the なん (nan) is used to explain something, give reasoning, or to emphasize something but in これはなんですか (korewanandesuka) the なん is treat as "what", how to know when to use, is it all depends on context?
Simply, see what comes before なん. If the word before な is something that can take だ/です (a noun, a na-/no-adjective, そう, よう, いつ/だれ/どこ/etc), it means this な is a form of だ and ん is an explanatory-no. See the usage pattern of explanatory-no here.
- 学生なんですか。 So you're a student?
- 簡単なんですか。 So it's easy?
- だれなんですか？ Who is it?
If なん is preceded by は or nothing, なん is "what".
- これはなんですか。 What is this?
- なんですか。 What (is it)?
EDIT: As broccoli said, は is commonly omitted in casual speech, which makes a sentence like それなんですか technically ambiguous.
- So it's that one (that we are talking about)!
- What is that?
But a comma is usually inserted after それ if the latter meaning is intended. Even if there is no comma, you can easily tell the intended meaning from the context.