We present a measurement of the helicity of the W boson produced in top quark decays using ttbar decays in the l+jets and dilepton final states selected from a sample of 5.4 fb^-1 of collisions recorded using the D0 detector at the Fermilab Tevatron ppbar collider. We measure the fractions of longitudinal and right-handed W bosons to be f_0 = 0.669 +- 0.102 [ +- 0.078 (stat.) +- 0.065 (syst.)] and f_+ = 0.023 +- 0.053 [+- 0.041 (stat.) +- 0.034 (syst.)], respectively. This result is consistent at the 98% level with the standard model. A measurement with f_0 fixed to the value from the standard model yields f_+ = 0.010 +- 0.037 [+- 0.022 (stat.) +- 0.030 (syst.) ].