Two years ago I read a thesis written by a law graduate in Catalonia, which dealt with these questions.
There are three legal theories on this:
Spain continues to exist as a state, while Catalonia is a new state. In this case Catalonia would not inherit any of the treaties that Spain has signed, but would start with a clean slate. That would mean Catalonia would initially lose its EU membership and need to reapply. Among others, this requires unanimous approval by all present members—which might get blocked by Spain as well as by a few other member states who are afraid of secession movements within their own borders.
Both Catalonia and the remainder of Spain are considered successors of the current Spanish state, and would thus both inherit the treaties which Spain has signed up until that point. That would make both Catalonia and Spain EU members from Day One—in fact both might be considered to have been members before.
The current state of Spain ceases to exist, and both Catalonia and Spain without Catalonia are considered new states. In this case, both would have to re-negotiate any previous international treaties, which would mean they would both drop out of the EU.
There are precedents for each of these, but none involving the EU. The EU’s stance currently seems to be that they would treat it as #1. Some member states (those who see a risk of themselves losing territories to an independence movement) would likely favor #1, while those who regained their independence in the last few decades (the Baltic and ex-Yugoslavian states) might hold different views.