We present an immersed boundary (IB) method for 3D simulation of flappingflags in a uniform flow. The proposed formulation is manipulated on the basis of an efficient Navier-Stokes solver adopting the fractional step method and a staggered Cartesian grid system. A direct numerical method is developed to calculate the flag motion, with the elastic force treated implicitly. The fluid motion defined on an Eulerian grid and the flag motion defined on a Lagrangian grid are independently solved and the mass of flag is handled in a natural way. An additional momentum forcing is formulated from the flag motion equation in a way similar with the direct-forcing IB formulation and acts as the interaction force between the flag and ambient fluid. A series of numerical tests are performed and the present results are compared qualitatively and quantitatively with previous studies. The instantaneous flag motion is analyzed under different conditions and surrounding vortical structures are identified. The effects of physical parameters on the flapping frequency are studied.