Generally speaking, a court can force you to testify if you are under subpoena with the threat of jail. However, there are a handful of legal reasons that could excuse someone from testifying, including:
The testimony is incriminating. The Fifth Amendment of the U.S. Constitution gives you the right to avoid self-incrimination.
The testimony is privileged. Certain communications are considered privileged, including communications between a spouse, attorney, therapist or priest.
The witness is incompetent.
The subpoena was too broad, vague or not properly served.