So I have tried searching for the working of a rainbow. I found many sources and the explanation was clear. I still have a problem though. Since dispersion happens due to different refractive indices for different colours, splitting should take place always independent of the angle. Then why is it possible at 40-42 degrees only. My attempt was thinking that the reason could be that total internal reflection doesn't happen at other angles even if splitting happens but I couldn't quite understand it. How does this angle dependence of dispersion be understood?
There are two different assumptions that are not strictly correct in your question:
Firstly, a common misconception is that rainbows are formed due to Total Internal Reflection. This is not true. Indeed, as you point out, if it were indeed Total Internal Reflection, there would be a priori no reason for all rainbows to have the same angle from the line joining the sun and the anti-solar point.
As I have explained in my answer to this question: Difference between primary and secondary rainbow, the rainbow appears at certain angles because spherical drops have an angle of minimum deviation (which depends on the refractive index). Light rays that strike the drop close to this angle are "bunched" together, and thus are of a sufficient intensity for you to see them with your eyes. If they were not so closely bunched together, your eyes would not recognise them as a "band" of some colour.
Secondly (and perhaps more to the point of your question), the refractive index of a material is dependent on the wavelength of light (indeed, this is the principle by which the prism operates!), see here and here for more on this. As a result, this angle of minimum deviation also has a dependence on the wavelength (or "colour") of light, and this is what leads to different colours appearing at different angles.
As I mentioned in the other answer, check out Jearl Walker's amazing article explaining how the rainbow is formed, and why the secondary rainbow has its colours "flipped"!
In a primary rainbow, a light beam from the sun enters one side of a raindrop, reflects from the back, and leaves from the other side. Due to variations in the index of refraction, the angles will be slightly different for different colors. If you plot the deviation angle (between the incident beam and the one which leaves) versus the incident angle (measured from the normal to the surface) you find that the curve has a minimum (near 140 degrees deviation and 60 degrees incident). At this minimum, the deviation changes very little for small changes in the incident angle. This means that at this angle, the drop can take beams from different parts of the sun, and concentrate them into one emerging beam (this concentration angle is different for each color, and you see the colors that have been concentrated.)