Events with an isolated lepton and missing transverse momentum and measurement of W production at HERA