"If only the hanging mass affects the acceleration of the entire
system,"
That's not true. Both masses affect the acceleration of the system. The weight of the hanging mass is the only external force acting on the system. If you change $m_1$ it's true you won't change the net force on the system, but you will definitely change (affect) the acceleration.
According the Newton's 2nd Law, the net force acting on a mass must be equal to the time derivative of the momentum. In constant mass systems, we often replace that time derivative with mass times the acceleration of that mass. This relationship holds regardless of the individual forces acting on the mass and whether they are internal or external to the system. In this case, there are 3 forces acting on $m_1$:
- the tension force of the string pulling parallel to the table top,
- the weight of $m_1$ acting down, and
- the normal (contact) force from the table top pushing upward.
The only acceleration observed is parallel to the table top, so the upward and downward forces must add to zero. Therefore, the tension force must be equal to $m_1\vec{a}$.
Notice that the tension force itself is NOT the same physical item as the product $m_1\vec{a}$. Mathematically, they are equal, but physically they represent different things. One is a force vector, and the other is an acceleration vector multiplied by a scalar. This is easier to understand if you reverse the traditional equation:
$$\vec{a}=\frac{1}{m}\sum_j \vec{F}_j.$$
Here we are adding several force vectors, none of which are the same as an acceleration. After adding them and dividing by the mass we should get the same result as the single acceleration vector which we observe.