Abraham (Abram) did as his wife asked of him (the Hebrew translates to “beseech” or “pray”) and went to Hagar. Unlike Sarah (Sarai), Hagar becomes pregnant. As Sarah was focused on Abraham having children (perhaps more so than Abraham, it seems), you would think she would be satisfied. Of course, with a son being the focus of the entire arrangement, it altered the relational structures. Now Sarah’s desires became in conflict with the implicit power of Hagar and the son.
Abraham would, of course, treat Hagar differently. He’d had intimate contact with her, and she was the mother of his son. Based on Abraham’s response to Sarah, though, there was still a recognition that this was still not alright.
In many respects, Abraham (even though he was honoring his wife’s plea) was unrighteous in what he did, at least from our perspective. It was common at the time, though the Scriptures do say, one man and one woman.
Yet, Genesis records God considering Abraham as righteous. Paul brings it up, too. The key is that Abraham is credited as righteous (or as Paul states, made righteous). That doesn’t make Abraham righteous in one sense. God “made” or “considered” Abraham righteous, so Abraham was now righteous.
Paul’s point is that we, like Abraham, are now righteous not because we are, but because we believe in Jesus Christ. It’s humbling—or it should be—that the righteousness that we (should) cling to is not ours. It is a gift of grace.