In my textbook there is given a case that to calculate acceleration and tension produced in string when string is attached at it's ends with two bodies of masses such that $m1>m2$ and the tension produced at both ends as reaction to the weight is $T$. How can be tension equal at both ends however the acting force (weight of the bodies) are different?
The tensions are not "reactions" in the sense that they are not the 3rd law partners of the weights of the two masses, so they don't have to be equal to the weights. For the tension to be the same throughout the string, it must be 'light', i.e. massless. Because this equal tension can't possibly balance BOTH the two different weights, each mass will accelerate because each will have a non-zero resultant force.
If the tensions were each equal to the weights then the system would not accelerate and the string could not be massless. However, it is usually safe to assume in a textbook style question unless otherwise stated that the string is massless, but bad form not to mention this fact!
In a simple model of a rope or string, one assumes that it is massless (or 'light'). The effect of this, in terms of the model, is that the tension must be the same throughout the rope. To model the rope as having mass, you have to consider a typical element/piece of the rope, and examine the forces on this piece from either side; calculus is needed to get the full dependence of the tension on distance along the rope, but you can consider a simpler similar problem. 3 train carriages of equal mass $m$ being pulled along a frictionless track from one end by a constant force $F$(to the right, say), with the tensions $T_1$ and $T_2$ between each pair (left to right) carried by tow bars or similar. Now the right carriage will have a net force $F-T_2$ to the right, and hence an acceleration $F-T_2\over m$ to the right. The middle carriage will have a net force $T_2-T_1$ to the right, and hence an acceleration $T_2-T_1\over m$ to the right. The left carriage will have a net force $T_1$ to the right and an acceleration $T1/m$ to the right. Now if we know they are rigidly fixed together, their accelerations must be equal, and if you solve the resulting equations you should get that $T_1=F/3$ and $T_2= 2F/3$, that is the tension nearest the pulling force F is twice as large as the next one along. The only way you get different tensions is if you allow the carriages to have no mass. That is, if the rope (or train of carriages) has mass, then the tension will vary along the rope, but if it's massless then it will have a constant tension.
That is a really good question that usually books fail to address in an intuitive way. Let me give you a step by step intuitive explanation by looking at the Atwood's machine:
Start with the balance of forces around the cable:
F2 is greater than F1 since the cube has more mass than the sphere. Now we use the common assumption that textbooks use, that is, assume that the cable's mass is insignificant: $$m = 0$$
So the first equation would yield:
$$𝑭_𝟐−𝑭_𝟏=0(=) 𝑭_𝟐 = 𝑭_𝟏 $$
What is strange is that we are applying different forces at the cable's ends, however, the cable "feels" two equal forces. But we clearly know that F2 and F1 aren't the same. So what is this force that the cable "feels" at its extremities? Let's call it tension, T:
Since the cable is "feeling" two equal forces, T, at its extremities, then it will react against the bodies it is attached to, with also a value of T. That's why in the Atwood's machine from the first figure, a tension T is applied against each body. Remember this is only true with the initial assumption (m=0), and a more realistic approach is mentioned in danimal's answer.
Of course, you now can calculate what is the value of T by analyzing the FBD of the sphere and the cube. I'll leave that up to you.
Clue: assume that both bodies have the same acceleration: if the position of the cube goes downwards 1 unit, then the position of the sphere goes upwards also 1 unit (thanks to the cable which has a constant length). Since the displacements (absolute value), $r(t)$, of the bodies, are the same over time, then their derivatives are also the same, meaning that $a_1 = a_2$, where $a_i$ is the resultant acceleration on body i.
The string is obviously assumed to be ideal. An ideal string does not stretch or expand. And since the whole string going around the pulley is one string it must experience the same force on both the ends. If one end has even a slightly more tension then the other than the string would tend to elongate. Plus after the elongation it would again have same tension on both sides.