Tension of rope. Different Answers? The tension of a rope in a tug of war with unequal forces is equal to the least force on the rope. Am I right? (If group A pulls the wire with 20 newton leftwards and the group B pulls with 30 newton. I expect that the tension is 20 newton.) 
But the tension on the rope pulling a mass vertically upward is said to be equal to $mg+ma$.( where $m$ is the mass of the object being pulled, $a$ is the acceleration of the object)
How come the same situations bearing different answers?
 A: 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. 
A: 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.
A: @user121330 
The whole system moves with 400 N rightwards. And the force acting on both the ends of the rope is 200 N. So that tension becomes 200 N
