I can understand Spanish but I have a hard time speaking it. Why is that?

It is typical in language acquisition for comprehension to precede production. That is, understanding someone speak is almost always easier than speaking yourself. This is because of three things: (1) it takes a while for your mind/brain to build up enough language in it for use during production; (2) the mechanisms needed for production are different from the mechanisms used to comprehend someone else; and (3) you are able to use a lot more strategies in real time when trying to understand someone compared to what you can do when the pressure is on you to speak yourself. That is, you can fill in gaps and guess much more easily when comprehending someone than when speaking.

If you watch children learning a first language, the same is true for them: their ability to understand people around them far exceeds what they can say at a given time.  As we’ve said before, speaking will emerge with time. Concentrate on trying to understand as much as possible when you hear Spanish. Listening (and reading, too!) will help you build up the linguistic system you will need later on for production.