Preservation of lineage is one of the most important objectives (maqasid) of shari'ah. Since modern technology permits things that people in the past could not even imagine, the rulings in these cases must be derived by analogy and wisdom, keeping in line with the objectives of shari'ah.
When the sperm donor is not the husband, this is unquestionably impermissible because one cannot have children with someone other than their wife. The question of lineage becomes murky, and there are all chances of someone's rights being infringed upon (either the child who is entitled to know their lineage and entitled to interaction with the father, the mother who is entitled to child support, or the father who is entitled to his progeny). In addition this whole idea goes against the cohesion of the family unit and in fact disturbs it.
When the sperm donor is the husband and the egg is that of the wife and fertilization occurs outside the womb but the fertilized egg is implanted inside the wife, this is permissible as a case of last resort.
When the fertilized egg is implanted into another woman (a surrogate) this is impermissible as well because it is as if motherhood is shared between two people and one necessarily loses their right (I mean motherhood not only in the genetic sense, but in the sense of all the hormonal changes the pregnant woman goes through and the emotional (and physical!) bond between her and the baby).