By this stage in the play John's getting pretty desperate. The way things are going both he and his wife Elizabeth are heading for the gallows. So John somehow has to undermine Abigail's credibility as an accuser. Getting Mary Warren to spill the beans didn't work so he openly admits to having had an affair with Abby.
John's confession is supposed to provide the court with a rationale for Abby's making all these false accusations. She's a scorned woman who wants revenge. In Puritan communities at that time there was a double standard when it came to adultery. Although it takes two to tango, as it were, the woman involved was always blamed for any affair, and would be labelled a "scarlet woman," or a "temptress." John knows this, which is why he reveals the affair.
Unfortunately for John, his gameplay fails miserably. Elizabeth, thinking that she's protecting her husband's reputation, denies that such an affair ever took place. Unwittingly, she's just made John look like a complete liar and fantasist in front of the whole court.