Watching a trial on TV can make it seem free form, like people can say anything they want on the witness stand. In real life trials, there are strict rules about what can and cannot be said. Rules, called the Rules of Evidence determine what can and cannot be said. Generally what is said by someone outside of court is called hearsay and cannot be said in court, this means that you cannot say ‘my friend saw that guy driving way too fast’, you can only comment on someone else’s driving if you saw it yourself.
Another key rule is relevance, what is said in a trial has to relate to the issue of the trial. If someone is on trial because they are accused of stealing money a witness cannot say that they drove drunk, whether they drove drunk is not legally relevant to whether they stole money.
When a person is on trial accused of a crime, or involved in a legal disagreement of any kind, their whole life history is not put on trial, what is said has to relate to what they are accused of, or the disagreement that the case is about.