Make g the subject of the formula, T = Mg - ma

Add m a to both sides

T m a = M g

Divide both sides by M

( T m a ) / M = g

g = ( T m a ) / M