Genesis 38 does not start off well. It does not end well. And, honestly, the stopping point at Genesis 39:18 isn't a particularly happy moment, either.
These narratives demonstrate the use of sexuality as a weapon. For Judah, Onan, and Shelah, it was an economic weapon: Onan declined his responsibility to his brother, Er, and Judah kept back Shelah as if the problem had been Tamar in the death of Er and Onan.
It wasn't. Both of these men died because of their own sin--Er being the first person recorded as being "put to death" by YHWH for his evil behavior. Onan's sin is debated these days, and his death is given as the reason to avoid masturbation or birth control, though neither are rightly condemned from this passage. This is explicitly about his decision to serve his own needs.
Also worth noting is this: Genesis is not speaking about the morality of what is called "levirate marriage." That is, this passage is not about the whether or not the cultural practice of a childless widow being expected to bear a child through her brother-in-law. It was a common practice of the time, and is codified in the Law given at Sinai (see Deuteronomy 25). I would suggest that it was a common practice which was regulated in the Law, somewhat as slavery was, because it was going to happen--note that the Deuteronomy passage establishes the rights of the woman in the situation--not because it was a great idea. Since it would happen, there needed to be safeguards, and the Law established those.
Back here in Genesis, though, the big concern is Tamar: what will happen to her? She is left in her father-in-law's home, living as a widow and awaiting Shelah as a husband. She's not free to move on to another husband, and she's not getting what she needs from Judah. She then turns to the only option she finds available: Judah. He is the one who is holding her, effectively, hostage. So she uses his own lusts against him, resorting to the only thing she has available.
And before we condemn her, we need to ask the question: what outlets are available to those on the outside of power in our society? If someone has no recourse within the law or respectable opinions, then who are we to decry their actions outside of those places? Tamar had nowhere else to turn--at the word of her father-in-law, she and her unborn child were nearly executed! If there are no good options for justice, people will turn to the least bad option available to them.
Tamar goes on to see two sons born, Zerah and Perez, and Zerah is in the lineage of Jesus. There is little else about Tamar, but she stands as an example of the disaster that powerful men can wreak on other people's lives.
The next chapter gives us the disaster that powerful women can wreak on other people's lives, as we see Joseph imprisoned by the false accusations of Potiphar's wife. Joseph has worked hard, acted with integrity, and still goes to prison. Again, we see the lack of a valid system for justice: in this case, Potiphar holds power and his wife wields that power against Joseph.
In the long run, though, Joseph does have an out. Pharaoh can do something about the prisoners in Egypt--he just needs to know about the problem. And Pharaoh does not like to hear about problems, so it will take some time.
What do we learn from this?
First, the clearest lesson in both of these cases is that justice should never be an "option." It should be something that is readily available for all.
Second, let us be careful about being quick to condemn others. Note that both Tamar and Joseph are condemned for actions without the full story--and that condemnation came from some corners that were hardly righteous. Give time for someone to explain themselves, and then throw those into prison who deserve it.
Finally, be careful of judging people based on their heritage alone. I've been in small to medium Southern American towns most of my adult life, and in very few of them could Perez or Zerah have gotten a fair shake. Simply through their birth under a cloud of controversy, they would have been watched more closely at school, overlooked at churches, and sidelined in the community. We must be careful not to quickly judge someone because of who their parents were--for good or for ill. Let people stand on their own.