Well, to answer your question, we have to go back to the 18th century when many writers began writing plays which were of a high moral nature. Characters overcame their flaws or fallibilities through events which could be considered moral trials. These plays termed Sentimental Comedies, were meant to evoke tears rather than laughter.
The main characters in these stories were typically middle class, moral individuals who were led astray by questionable, tempting trials. Importantly, these characters could be returned to the straight and narrow through proper example and fortitude. Characters in this genre were distinctly classified as good or bad. Typically there would be a test of trials which led the spectators to tears, yet held the anticipation of a happy ending through the restoration of the character's true virtue.
The Beggar's Opera, by John Gay, was written in an attempt to expose the deceit in the Court of London including the lawyers, judges, politicians, and other officers of the Court and Newgate Prison. Mr. Peachum serves as both a dishonest thief and informer. His home is an analogy of sorts of the Courts of London and what was happening at that time with members being both deceitful thieves and justice enforcers.
In Newgate Prison, Lockit, the jailer is easily bribed. The power of money is an important topic in Gay's opera, exposing class distinction and the partisan element of the penal system. In keeping with the Sentimental Comedy genre, the play ends with the Macheath character being released from prison rather than put to death, ensuring a happy ending as required in the Italian satirical Operas. As usual, eNotes says it best (link included below):
In a satirical turn on the fashionable Italian opera, Gay uses this final scene at Newgate as a departure from the “realism” achieved in the production. Macheath is allowed to live so that this beggar’s opera can follow the Italian opera’s convention of the contrived happy finale.
In conclusion, due to the tears involved in the tests of trials of The Beggar's Opera, by John Gay, and yet with the anticipation of the happy ending that does, in fact, happen when Macheath lives instead of dies; this shows the true adherence to the ideas behind the Sentimental Comedy.