What is the difference between
How could it had happened
How could it have happened
It would be better if anyone can show its usage in some kind of situation.
John Lawler's comment provides a complete answer:
Could is a modal auxiliary and modal auxiliaries must be followed by an infinitive form of the next verb. Have is the next verb, and the infinitive form of have is have. Not had. That's all.
“How could it had happened” is therefore not a grammatical utterance.