There are two reasons for this.
First, the census was not conducted in accordance with the Law, which called for a "ransom" to be paid by each person enrolled.
Exodus 30:11–16 (Masoretic Text - JPS Tanakh)
The LORD spoke to Moses, saying: When you take a census of the Israelite people according to their enrollment, each shall pay the
LORD a ransom for himself on being enrolled, that no plague may come
upon them through their being enrolled. This is what everyone who is
entered in the records shall pay: a half-shekel by the sanctuary
weight—twenty gerahs to the shekel—a half-shekel as an offering to the
LORD. Everyone who is entered in the records, from the age of twenty
years up, shall give the LORD‘s offering: the rich shall not pay more
and the poor shall not pay less than half a shekel when giving the
LORD‘s offering as expiation for your persons. You shall take the
expiation money from the Israelites and assign it to the service of
the Tent of Meeting; it shall serve the Israelites as a reminder
before the LORD, as expiation for your persons.
Furthermore, the people of Israel do share responsibility, since it was they who insisted on having a king installed over them in the first place, even though the Lord warned them through Samuel that it would bring misfortune upon them (1 Samuel 8:6-19).
Gregory the Great wrote on how rulers behavior often reflect the hearts of the rules, commenting in the context of this census:
The characters, then, of rulers are so assigned according to the
merits of their subjects, that frequently they who seem to be good are
soon changed by the acceptance of power. As holy Scripture observed of
the same Saul that he changed his heart with his dignity. Whence it is
written, When you were little in your own eyes, I made you the head
of the tribes of Israel”[1 Samuel 15:17]. The conduct of rulers is so
ordered with reference to the characters of their subjects that
frequently the conduct of even a truly good shepherd becomes sinful as
a result of the wickedness of his flock. For that prophet David, who
had been praised by the witness of God himself, who had been made
acquainted with heavenly mysteries, being puffed up by the swelling of
sudden pride, sinned in numbering the people. And yet, though David
sinned, the people endured the punishment. Why was this? Because in
truth the hearts of rulers are disposed according to the merits of
their people. But the righteous judge reproved the fault of the sinner
by the punishment of those very persons on whose account he sinned.
But because he was not exempt from guilt, as displaying pride of his
own free will, he himself endured also the punishment of his sin. For
that furious wrath which struck the people in their bodies prostrated
the ruler of the people by the pain of his inmost heart. But it is
certain that the merits of rulers and people are so mutually connected
that frequently the conduct of the people is made worse from the fault
of their pastors and the conduct of pastors is changed according to
the merits of their people.
Morals on the Book of Job XXV.16