Comparator output not fully switching on MOSFET
the maximum voltage output is around 3.3V and not at 5V. How would you
  explain that ?


The LM311 has an open collector output so the it does not "drive" the output high.  For your circuit, the output high is given by:

$$V_{OH} = 5V(\dfrac{2.7k + 1k||2.7k}{2.97k + 2.7k + 1k||2.7k} + \dfrac{1k}{1k + 2.7k}\dfrac{2.97k}{2.97k + 2.7k + 1k||2.7k}) = 3.31V$$


  how would you explain that the output is not exactly at 0V when the
  input is high?