I think your concept of tension needs clarification. "The tension" is not a well-defined concept for a rope with non-zero mass. One has to be careful to specify where the tension is being considered.
We take the system in question to be the rope having mass $m_\mathrm{rope}$. There are two forces on the rope, one the pull from group A the other the pull from group B. Tension is the force applied by a "rope-like" agent on some other object. The word tension can also describe the force of the object on the rope, as in this case. (More generally, the "rope-like" agent can be anything that can apply a tensile force.)
In the tug-of-war, the tension due to group A is $T_A = -20\,\mathrm{N}$ (minus sign to indicate the tension is to the left, while the tension due to group B is $T_B = 30\,\mathrm{N}$ (to the right). Then Newton's second law requires $$F_{net} = T_A + T_B = m_\mathrm{rope}a_\mathrm{rope}$$
Note that this is the same form as your second example. However, here the system is the object hanging from the rope, not the rope itself. The tension under consideration is between the end of the rope and the object, so the mass of the rope is immaterial.: $$F_\mathrm{net} = T + F_\mathrm{gravity} = ma$$ $$T -mg = ma$$ or $$T = mg + ma$$
In any event you can't speak about "the tension" in a massive rope as if it's just one value. But as you can see from the first of my equations, if the rope is massless then the tension on either side is the same, and one might talk of "the tension". Massless ropes are, however, idealizations that don't exist in nature.
Update: More Details
You can't ignore the mass of the rope. The tension on the right is 30 N, the tension on the right is 20 N. The rope can't be massless, because then the acceleration would be infinite.
To analyze this a little more deeply, we'll first take the system in question to be the entire rope. Then we'll look at a point somewhere along the length of the rope. Take the mass of the rope to be $m$, and its length to be $L$, and the linear density of the rope to be constant: $\lambda = m/L$.
The tension on the right end of the rope is $T_R$, the left $T_L$ (one of them is algebraically negative). Our system, the rope, has two forces on it. Newton's Second Law tells us the acceleration:$$a=\frac{F_\mathrm{net}}{m} = \frac{T_R + T_L}{m}=\frac{T_R + T_L}{\lambda L}$$
Now lets take a new system: the portion of rope from the left end to some point $x$ meters from that end. For example, if we take exactly half the rope, $x=L/2$. The tension on the left end is still $T_L$ $\ldots$ after all, nothing has changed physically. We're changing only our analysis. For the same reason, we also know the acceleration: it's given by the expression above. Call the tension on the right side $T'_R$. The net force on the segment is $F = T_L + T'_R$.
Now we can find the net force on our new system. The mass of the new system (a portion of rope from the left end having length $x$) we'll call $m_x$. With that, $m_x = \lambda x$, and the net force on the portion is
$$ F = m_xa = m_x\frac{T_R + T_L}{\lambda L} = \frac{x}{L}(T_R + T_L)$$
The tension force on the right side of the segment is $$T'_R = F-T_L = \frac{x}{L}(T_R+T_L)-T_L$$
So you can see that the tension on the right depends on where you measure it. There is no "the tension". It varies along the length. At the left end, $x=0$ and $T'_R = -T_L$. At the right end, $x=L$ and $T'_R = T_R$. Both of those are as expected. The tension varies linearly along the length.