Kris's answer is correct, and provides a general explanation of the difference between the words "that" and "which." I would like to add that in most contexts, "which" acts as a coordinating conjunction and requires a comma before it ("This book, which is my favorite..."), while "that" can function something as a pronoun ("That is why..."), an adjective ("That dog..."), or a coordinating conjunction that typically does not allow a comma before it ("I chose the book that was my favorite..."). Due to the non-restrictive nature of a relative clause introduced by "which," the clause is considered a parenthetical, and therefore must be enclosed by commas, parentheses, or dashes. The lack of a comma before "that" helps indicate that the relative clause is necessary to fully specify the noun phrase, and is therefore a dependent clause tied to the preceding clause. So in most cases, "which" requires a comma, but "that" cannot have a comma before it.
In your example, however, "I have flunked the exam, that is why I am attending coaching classes" is a comma-splice. This is because "that" must connect to the noun immediately preceding it, which in this case is the exam, not the fact that the speaker flunked the exam (which is what "which" modifies, but we can only know this because it doesn't make sense to assume that "which" modifies "exam"--syntactically, it's impossible to tell). Since "that" does not connect to "exam," the second clause, "that is why I am attending coaching classes," is an independent clause (hence the suggestion in Kris's answer that "that" should be used to start a new sentence), and therefore cannot be joined to the preceding clause with only a comma.