To cast a spell you have to have the required ability score, and be of sufficiently high level. Each spell casting class has a chart stating when, and how many spells they can cast. You have to meet both requirements to be able to cast a spell. For example to cast a 4th level bard spell you have to have a CHA of at least 14, and be a 10th level bard.
You also have to know the spell in question. Generally spontaneous casters have a fixed number of spells they know based on level and can’t learn any more. Prepared arcane casters usually have some method of recording their spells like a book, or familiar that they need to memorize a spell. Prepared divine casters on the other hand usually know all spells on their list for spells they are high enough to cast.