First, to be clear, this is almost never a state level limitation. To the extent that the link in the question implies otherwise, it is incorrect.
Instead, local governments, typically a municipality or in an area that is not part of any municipality, a county, enacts land use regulations, the most common of which is usually called a "zoning code." There are also usually locally adopted building codes that supplement the zoning code.
A zoning code typically establishes rules regarding how different kinds of real estate can be used in different enumerated zones. Usually, there are several different zones in which residential uses are allowed and different rules apply in each one. One of the typical limitations in a residential zone is to establish the number of unrelated people who don't own the home who may live there.
The purpose of such limitations is to discourage the establishment of boarding houses, group homes, women's shelters, homeless shelters, flop houses, and halfway houses in relatively low density residential zones, on the theory that these uses are undesirable in terms of neighborhood character and cause undesirable people to move into a neighborhood that lowers property values for other people who live there.
More crassly, people in affluent neighborhoods want to ban property uses that make it possible for poorer people to live near them.
Often the actual scope of the limitations, however, is far broader than is necessary to prohibit these particularly unpopular "NIMBY" (not in my backyard) uses. The drafters of zoning codes are frequently more concerned about preventing loopholes in their restrictions from being identified than about limiting property rights in a respect that most current property owners in the zone don't avail themselves of anyway.
For example, in Denver, where I live, the City Council recently passed a Group Living Amendment to its zoning code easing restrictions on uses of residences by unrelated people. This measure was sufficiently controversial that members of the public used their referral powers to petition to have the repeal of the recently enacted legislation places on the November 2021 general election ballot for voters to consider repealing it.
As the link in the question notes, homeowner's associations, frequently also impose limitations on this kind of use of a residence, which are often even more strict, and are frequently adopted for the same reason. HOAs have more authority to do so than local governments because as private entities, they aren't required to meet constitutional law limitations on their authority.
The question is, if both parties agree to forgo any safety measures
that it might be there to protect, who exactly is it hurting if one
person pays the other to stay over?
In simple terms: why is it illegal? Why does the government care who
stays in who's house, while paying money? Is there some 3rd party that
is negatively affected if the guidelines aren't followed?
The third-party is the neighboring property owners who fear that the quality of their neighborhood will be reduced by this kind of agreement.
I am sympathetic to these criticisms. But, they aren't barriers to enacting laws if people with the power to enact legislation disagree.
The simple rule is that in the real world, legislation is not required to adhere to a liberal/libertarian ideal in which laws are enacted only to regulate matters in which someone else may arguably be harmed. Subject to light constitutional regulation, the authority of local government's to enact legislation regulating the use of property is frequently plenary (i.e. unlimited) or close to it. The government can do anything it wants that it isn't forbidden from doing by the constitution or a higher level government's statutes. Courts aren't permitted to second guess the accuracy or desirability of these legislative determinations in most cases.
Many states, furthermore, have "home rule cities" that are expressly protected from having their legislative authority limited in many respects by state legislation.
A regulatory taking, the prevents property from having any beneficial use, must be compensated by the government enacting the regulation of its use. But regulatory laws may reduce the value of property by limiting the uses to which it can be put so long as there is still some beneficial use of the property that remains available.
A very light and disputed impact that is perceived to be negative, such as the one in this case, is a sufficient rational basis for a municipal or local government to enact such legislation, and many local government zoning codes do, in fact, regulate this kind of activity.
The U.S. Supreme Court upheld the authority of local governments to enact this kind of legislation in the United States in the face of a constitutional challenge to it, in the case of Village of Euclid, Ohio v. Ambler Realty Co. in 1926, although some more extreme versions of limitations on non-related people sharing a home have subsequently been held to be invalid, mostly as a result of later enacted federal fair housing laws.