As a general rule, subject to the subject matter jurisdiction of the court considering the case, it is permissible to bring a counterclaim on any matter, whether or not related, between the same parties. But, a counterclaim would not be allowed or mandatory if the court doesn't have subject matter jurisdiction over the counterclaim (although it might be possible to transfer the entire case to a court that does have that jurisdiction).
For example, it would not be permissible to bring a counterclaim seeking a divorce or annulment in an action in New York City Court since jurisdiction over divorces and annulments is limited to the Supreme Court (i.e. the trial court of general jurisdiction) in New York State.
Likewise, you couldn't bring and wouldn't be required to bring a counterclaim for copyright infringement in state court because the federal court has exclusive jurisdiction over copyright claims.
It is mandatory to bring a counterclaim on any related matter, and usually, if both the claim and the defendant's claim arise from a rental of the same apartment by the same person, that would be a mandatory counterclaim.
But, if there were a counterclaim against the landlord arising from an automobile accident many miles away from the rental property, for example, that would be a permissible counterclaim, but not a mandatory one.
When in doubt, the safer course of action is usually to file a counterclaim rather than a separate claim, because if a later court decides that the counterclaim was mandatory, a new lawsuit on the defendant's claim is barred under the doctrine of res judicata.