Rambam explains the reason for the laws of ritual impurity in Guide for the Perplexed 3:47.
THE precepts of the twelfth class are those which we have enumerated
in the section on "Purity" (Sefer tohorah). Although we have mentioned
their use in general, we will here offer an additional explanation,
and [first] fully discuss the object of the whole class, and then show
the reason of each single commandment, as far as we have been able to
discover it. I maintain that the Law which was revealed to Moses, our
Teacher, and which is called by his name, aims at facilitating the
service and lessening the burden, and if a person complains that
certain precepts cause him pain and great trouble, he cannot have
thought of the habits and doctrines that were general in those days.
Let him consider the difference between a man burning his own son in
serving his god, and our burning a pigeon to the service of our God.
Scripture relates, for even their sons and their daughters they burn
in the fire to their gods (Deut. xii. 31). This was the way in which
the heathen worshipped their gods, and instead of such a sacrifice we
have the burning of a pigeon or a handful of flour in our worship. In
accordance with this fact, the Israelites, when disobedient, were
rebuked by God as follows: "O My people, what have I done unto thee?
and wherein have I wearied thee? Testify against me" (Mic. vi. 3).
Again, "Have I been a wilderness unto Israel? a land of darkness?
Wherefore say my people, We are miserable; we will come no more unto
thee" (Jer. ii. 31); that is to say, Through which of the commandments
has the Law become burdensome to the Israelites, that they renounce
it? In the same manner God asks the people, "What iniquity have your
fathers found in me, that they are gone far from me?" etc. (ibid. ii.
5). All these passages express one and the same idea.
This is the great principle which you must never lose sight of.
After having stated this principle, I repeat that the object of the Sanctuary was to create in the hearts of those who enter it certain
feelings of awe and reverence, in accordance with the command, "You
shall reverence my sanctuary" (Lev. xix. 30). But when we continually
see an object, however sublime it may be, our regard for that object
will be lessened, and the impression we have received of it will be
weakened. Our Sages, considering this fact, said that we should not
enter the Temple whenever we liked, and pointed to the words: "Make
thy foot rare in the house of thy friend" (Prov. xxv. 17). For this
reason the unclean were not allowed to enter the Sanctuary, although
there are so many kinds of uncleanliness, that [at a time] only a few
people are clean. For even if a person does not touch a beast that
died of its own accord (Lev. xi. 27), he can scarcely avoid touching
one of the eight kinds of creeping animals (ibid. 29, seq.), the dead
bodies of which we find at all times in houses, in food and drink, and
upon which we frequently tread wherever we walk; and, if he avoids
touching these, he may touch a woman in her separation (ibid. xv. 18),
or a male or female that have a running issue (ibid. ver. 1, seq. and
25, seq.), or a leper (ibid. xiii. 46), or their bed (ibid. xv. 5).
Escaping these, he may become unclean by cohabitation with his wife,
or by pollution (ibid. 15), and even when he has cleansed himself from
any of these kinds of uncleanliness, he cannot enter the Sanctuary
till after sunset; but not being enabled to enter the Sanctuary at
night time, although he is clean after sunset, as may be inferred from
Middot and Tamid, he is again, during the night, subject to becoming
unclean either by cohabiting with his wife or by some other source of
uncleanliness, and may rise in the morning in the same condition as
the day before. All this serves to keep people away from the
Sanctuary, and to prevent them from entering it whenever they liked.
Our Sages, as is well known, said, "Even a clean person may not enter
the Sanctuary for the purpose of performing divine service, unless he
takes previously a bath." By such acts the reverence [for the
Sanctuary] will continue, the right impression will be produced which
leads man, as is intended, to humility.
The easier the diffusion of uncleanliness is, the more difficult and
the more retarded is its purification. Most easily is uncleanliness
communicated by the dead body to those who are under the same roof,
especially to relatives. The purification can only be completed by
means of the ashes of the red heifer, however scarce it may be, and
only in seven days (Num. xix. 11). The uncleanness caused by a woman
having running issue or during her separation is more frequent than
that caused by contact with unclean objects: seven days are therefore
required for their purification (Lev. xv. 19, 28), whilst those that
touch them are only unclean one day (ibid. vii. 18). Males or females
that are unclean through running issue, and a woman after childbirth,
must in addition bring a sacrifice, because their uncleanness occurs
less frequently than that of women in their separation. All these
cases of uncleanliness, viz., running issue of males or females,
menstruations, leprosy, dead bodies of human beings, carcases of
beasts and creeping things, and issue of semen, are sources of dirt
and filth. We have thus shown that the above precepts are very useful
in many respects. First, they keep us at a distance from dirty and
filthy objects; secondly, they guard the Sanctuary; thirdly, they pay
regard to an established custom (for the Sabeans submitted to very
troublesome restrictions when unclean, as you will soon hear);
fourthly, they lightened that burden for us; for we are not impeded
through these laws in our ordinary occupations by the distinction the
Law makes between that which is unclean and that which is clean. For
this distinction applies only in reference to the Sanctuary and the
holy objects connected with it: it does not apply to other cases. "She
shall touch no hallowed thing, nor come into the Sanctuary" (Lev. xii.
4). Other persons [that do not intend to enter the Sanctuary or touch
any holy thing], are not guilty of any sin if they remain unclean as
long as they like, and eat, according to their pleasure, ordinary food
that has been in contact with unclean things. But the practice of
the Sabeans, even at present general in the East, among the few still
left of the Magi, was to keep a menstruous woman in a house by
herself, to burn that upon which she treads, and to consider as
unclean every one that speaks with her: even if a wind passed over her
and a clean person, the latter was unclean in the eyes of the Sabeans.
See the difference between this practice and our rule, that "whatever
services a wife generally does to her husband, she may do to him in
her separation"; only cohabitation is prohibited during the days of
her uncleanness. Another custom among the Sabeans, which is still
widespread, is this: whatever is separated from the body, as hair,
nail, or blood, is unclean; every barber is therefore unclean in their
estimation, because he touches blood and hair; whenever a person
passes a razor over his skin he must take a bath in running water.
Such burdensome practices were numerous among the Sabeans, whilst we
apply the laws that distinguish between the unclean and the clean only
with regard to hallowed things and to the Sanctuary. (Friedlander translation, emphasis added.)
Essentially, there is nothing inherently bad or wrong or spiritually deficient about ritual impurity. It is either a physical uncleanness, or a legal state of being designed to keep one from frequenting the Holy Temple.