The move g5 in this position I would like to know what is the reason why g5 is a necessary move for Black. I now see that what I played Bd7 is horrible as it hangs a pawn on d6 after Qb4.

I cannot see why I should weaken the f5 pawn just to threaten the Queen which after Qb4 is out of danger.
I agree that f5 is irrelevant. A weakness is only a weakness if it can be exploited.

The main reason that g5 works here is that it can force the N away from f3, and then your N can go to d4 with not only a direct threat of Nxc2, which is VERY inconvenient to defend, and the more decisive threat of Nf3!. These threats are so strong that the computer thinks that Qd6 allowing Nf3 is best by two whole pawns.

It all comes down to forcing tactics that are favorable to black.

 [FEN "r1b2rk1/pp4qp/2ppn1p1/5p2/7Q/1PNP1N1P/1PP3P1/R3R1K1 b - - 0 1"]

 1... g5 2. Qb4 g4 3. hxg4 fxg4 4. Nd2 Nd4 5. Qxd6 Nf3+ $1 6. gxf3 gxf3+ 7. Kf2 Qg2+ 8. Ke3 f2 {Is crushing.}