Both, tangent space and world space represent a 3D space, just as inches and meters both represent length. If any meaningful calculations need to be done, all values must be in the same unit system or coordinate systems.
At this point you are probably wondering…
So why use tangent space in the first place? Why not just declare all positions and vectors in world space?
Certain per pixel lighting techniques and many other shaders require normals and other height information declared at each pixel point. This means that we have one normal vector at each texel and the n axis will vary for each texel. Think of it as a bumpy surface defined on a flat plane.
Now if these normals were declared in the world space coordinate system, we would have to rotate these normals every time the model is rotated even a small amount. Remember that the lights, camera and other objects also involved in these calculations will be defined in world space and will move independent of the object. This would mean thousands or even millions of object to world matrix transformations will need to take place at the pixel level. We all know matrix transformations don't come cheap.
Instead of doing this, we declare the thousands of surface normals in the tangent space coordinate system. Then we just need to transform the other objects (mostly consisting of lights, the camera, etc) to the same tangent space coordinate system and do our calculations there. The count of these objects won’t exceed 10 in most cases.
So it’s 1000000s vs 10s of calculations. Hmmm… It’s going to take a while to decide which one would be better.
Think of the box in figure 2 rotating. Even if we rotate it, the tangent space axis will remain aligned with respect to the face. Then instead of converting thousand or millions of surface normals to world space we convert tens or hundreds of light/camera/vertex positions to tangent space and do all our calculations in tangent space as required.
To add to the list of advantages of tangent space, the matrices required for the transformations can be pre calculated. They only need to be recalculated when the positions of the vertices change with respect to each other.
Hopefully you understand why we need tangent space and a transformation from world space to tangent space. If not, for now think of it as something that we just need to have and you will begin to understand when you read the second section.
In order to convert between coordinate system A to coordinate system B, we need to define the basis vectors for B with respect to A and use them in a matrix.
Every ndimensional coordinate system can be defined in terms of n basis vectors. A 3 dimensional coordinate system can be defined in terms of 3 basis vectors. A 2 dimensional coordinate system can be defined in terms of 2 basis vectors. The rule is that every vector that makes up a set of basis vectors is always perpendicular to the other basis vectors.
It’s basically one of those fancy mathematical terms given to three three vectors (in a 3D world) of unit size and perpendicular to each other. These three vectors can point in any direction as long as they are always perpendicular to each other. If you would like to visualize a set of basis vectors, think of an axis drawn in any 3D application. Look at Figure 1 & 2. The vectors that are on the axis (x, y, z) define one set of basis vectors. The vectors u, v, n also define another set.
And why all the fuss over these basis vectors?
In order to achieve our goal, the first step is to define the u, v and n basis vectors in terms of the world space coordinate system. Once we do that, the rest is just a cakewalk.
From here on, I will refer to the u, v, n vectors as the Tangent (T), BiNormal(B) and Normal (N). Why? Because this is what they are actually called.
As I said before we need to find the T, B, N basis vectors with respect to world space. This is the first step in the process.
A simple way of thinking of this would be:
I have a vector that points in the direction in which the u value increases across a face, then what will be it’s value in the world space coordinate system.
Another way of thinking of it would be:
To find the rate of change of the u, v, n components across a face with respect to the x, y, z components of world space. The T vector is actually the rate of change of the u component with respect to world space.
Whatever way you choose to visualize it in, we will have the T, B, N vectors.
The next step is to build a matrix of the form:

Tx 
Ty 
Tz 


Mwt = 
Bx 
By 
Bz 

Nx 
Ny 
Nz 
Multiplying a world space position (Posws) with Mwt will give us the position of the same point in terms of tangent space (Posts)
Posts = Posws X Mwt
As mentioned before the T, B, N vectors (and all basis vectors) will always be at right angles to each other. All we need to do is derive any two vectors and the third will be a cross product of the previous two. In most cases, the face normal will already be calculated. This means we just need one more vector to complete our matrix.
In this section I will take a single face and then derive Mwt for the face.
Figure 3: A sample face
Let’s assume the vertices of the face in Figure 3 have the following values

As stated in the previous section,
In order to derive T, B, N calculate the difference of the u, v, n components across the face with respect to the x, y, z components of world space.
Step 1: Calculate any two edge vectors. Edge
E21 = V2  V1 = (200, 2020, 00) & (10, 00) = (20, 0, 0) & (1, 0)
E31 = V3  V1 = (00, 020, 00) & (00, 10) = (0, 20, 0) & (0, 0)Edge E21 and E31 is actually the difference of x, y, z and u, v components between V1, V2 and V1, V3
Step 2: Calculate the T vector
In order to get the tangent vector T we need the rate of change the x, y, z components with respect to the u component.
Hence:T = E21.xyz / E21.u = (20/1 , 0/1 , 0/1) = (20, 0, 0)
The T vector is then normalized. This is done because we do not need the scalar component of the T, B, N vectors.
T = Normalize(T) = (1, 0, 0)
Note: This works fine for E21 in this case, but what if the E21.u was equal to 0. There is a good chance that this can happen, depending on how your artist has mapped the mesh. If the value of E21.u value is 0, then we perform the same calculation on E31. It does not matter which edge we use, all we are looking for is the direction of increasing u component across the face.
Step 3: Calculate the Normal vector (N)
Now that we have the tangent vector, we can get the N vector. This is a standard calculation for a face normal that we normally do.
A simple cross product of the edge vectors E21 and E31 can get us the face normal.
N = CrossProduct(E21 , E31) = CrossProduct ( (20, 0, 0) , (0, 20, 0) ) = (0, 0, 400)
And just as with the T vector, N is also normalized.N = Normalize(N) = (0, 0, 1)
Step 4: Calculate the BiNormal vector (B)
As mentioned earlier the T, B, N vectors are always at right angles to each other, hence we can derive the third from by just doing a cross product of the previous two. Therefore
B = CrossProduct ( T , N ) = CrossProduct ( (1, 0, 0) , (0, 0, 1) ) = (0, 1, 0)
Step 5: Build Mwt from T, B, N
Now that we have T, B, N, we can build Mwt
Tx
Ty
Tz
=
1
0
0
Mwt =
Bx
By
Bz
0
1
0
Nx
Ny
Nz
0
0
1
And volla! There we have our matrix.