Short answer.
In your example, the rope can not be massless (otherwise its acceleration would be infinite), but if it has mass, then tension is different on each point of the rope. Therefore you have to assume there is an object connected to it. Depending on which side of the rope you assume the object is, you get tension 20N or 30N.
This does not contradict the vertically lifted object by a rope. In that case the acceleration would be $a=\frac{T-w}{m}=\frac{T-mg}{m}$, that is $ma=T-mg$, that is $T=ma+mg$. What you are missing is that in this case $mg$ is the weight of the object, which does not affect the tug of war case (since its horizontal).
Long answer
Tug of war
If you pull the leftmost piece with 20N and rightmost with 30N, you would get 10N on a 0 mass object, meaning infinite acceleration. Therefore you have to assume there are two bodies (actually only one would be enough). So, one person pulling on each side of the rope.
Assuming the rope is massless, and is consisted of lots of tiny pieces, we can see that each tiny piece has two forces on it. $F_l$ from the left and $F_r$ from the right. Since acceleration of each tiny piece is $a=\frac{F_r-F_l}{m}$ and m->0 then $F_r$ = $F_l$, otherwise $a$ would be infinite.
Also, this means that any piece of the rope, including the ones that connect to the bodies, pull adjacent pieces with the same force. That is, body A and body B are both pulled by the rope with the same force $T$, which is the tension.
There is $F_A=20N$ on the left body and $F_B=30N$ on the right, plus $T$ and $-T$ respectively. Since bodies A, B and the rope have the same acceleration (if they didn't, they'd move apart from each other), we get:
$a=\frac{T-F_A}{m_A}=\frac{F_B-T}{m_B}$ , meaning $T=\frac{m_AF_B+m_BF_A}{m_A+m_B}$.
If $m_B$->0 then $F_B-T=0$, and $T=F_B=30N$. Likewise if $m_A$->0 then $T=F_A=20N$.
You can not assume that $m_B=m_A=0$ without $F_A=F_B$, therefore you need at least one non 0 mass object attached to the rope.
Upwards pulling
If $m_B=0$ then you get the same result as the vertically pulled body, excluding the weight $w$ of the object ($w=mg$), because when pulling horizontally its weight has no effect.
On the pulley on the left, there are 4 forces applied, $T_1'$, $T_2'$, the gravitational acceleration on the pulley (its weight) $m' g$ (directed downwards), and the tension of the rope at the center of the pulley $T$, which is the one that you draw, but directed upwards. Now, the tension $T$ balances the weight $mg$ and the other two tensions $T_1'$ and $T_2'$, and the pulley don't move.
However, the toques of the tensions $T_1'$ and $T_2'$ may not balance, and may result in a rotation of the pulley. In fact, if $L=I\omega$ is the angular momentum of the pulley, $I$ the momentum of inertia, and $\omega$ the angular velocity, one has
$$
\frac{d L}{dt}
=I\frac{d \omega}{dt}=r T_1'-r T_2'
$$
where $r$ is the radius of the pulley and the terms at the right side of the equations are the torques of the tension forces applied to the pulley.
If your problem is just to determine the static equilibrium of the system, and not its dynamics, you may want to assume $\frac{d L}{dt}=0$ and therefore balance the two torques $r T_1'=r T'_2$, that is, $T_1'=T_2'$.
Best Answer
The tension of the rope is the shared magnitude of the two forces. Imagine cutting the rope at a point and inserting a spring scale in its place. The reading will show the tension. A rope with zero tension would be hanging loosely or laying on the ground, neglecting the rope's mass.