- 田中さんは今日来ますか。
- はい、来ますよ。あ、田中さんが 来ました。
- Does Tanaka san come today?
- Yes, he does. Oh, Tanaka san came.
I don't understand the use of が in the second sentence.
When you describe or report information which is newly discovered by perception that doesn't include assumption or judgement, Japanese grammar requires you to express it as a sentence whose elements are not topicalized, in short, without adding particles like は.
That kind of sentences are called 現象文 or 中立描写文 (Sentence of neutral description). The example of あっ、田中さんが来ました is the case of it.
It can be a little difficult getting the translation of は and が right in English, but here's an approximate way that tends to be used:
The topic marker 「は」 marks the main focus of conversation. You can get a decent feel for what it's doing in a first-pass translation by translating it as "As for". For example:
田中さんは今日来ますか。
(roughly) As for Mr Tanaka (i.e. I don't care about other people), will he come today?
(more naturally) Is Mr Tanaka coming in today?
The subject marker 「が」 marks the subject of the current sentence, i.e. the person or thing that is doing the verb, but it means that the focus of the sentence isn't necessarily the subject - and sometimes (as in this case), the important part is the verb.
たなかさんがきました。
Mr Tanaka has come.
In other words, the use of が is because the important information isn't that we're talking about Mr Tanaka, but that he has already shown up (and so we don't have to wait around for him).