I am designing a 5 dof robotic arm, and for that I would like to find a torque equation. However, I am a little confused here: According to me the torque equation for M1 should be l2m2+l3m3+l4m4+l5m5 (m is mass of motors)(plus mass of link on their COG) but as these things will rotate so am I suppose to take their inertia or their mass?
It's actually quite a bit more complicated than that. You need to take into account not only the lengths and masses of each arm (and joint), but also what velocities and accelerations you want to support.
This paper provides a graded approach to understanding robot arm dynamics, starting with a simple dual-link arm.