Nowadays if you say "Juan tiene gracia" or "Juan es gracioso", everybody will understand that Juan is a funny guy. But it wasn't always this way. Let's go back in time a couple of millennia...
In the Latin language the word grātĭa meant, according to Lewis & Short:
grātĭa, ae, f. gratus; lit., favor, both that in which one stands with others and that which one shows to others.
I. Favor which one finds with others, esteem, regard, liking, love, friendship (syn. favor).
II. Favor which one shows to another, mark of favor, kindness, courtesy, service, obligation.
So grātĭa and favor were synonyms, and that explains why now you can say about a handsome person that they are both agraciados (if they are handsome) and favorecidos (if they look more handsome than usual). But let's keep on trace. As you see from the definition, the word could be used in a whole bunch of senses. For instance, you could show a gratia by giving something to someone without expecting anything in return, and the other person could say that they have received something gratiis (by gratia), which gave today's gratis (at no cost). And of course you say gracias to express gratitude.
If you were a graceful person you could say that you hace received a gratia (by nature, by god, by whoever gave it to you). But also you could give someone a gratia in many other ways. Let's move forward and see what Covarrubias (1611) says about the word. It stats by warning that it has many meanings:
- Gracia: it is the benefit we make or receive.
- Estar en gracia: to have the favor of a lord. When you lose that favor you say caer de su gracia (or caer en desgracia).
- Dar gracia a algo: to make something with elegance.
- Gracias: The indulgences of the pontiffs.
- Decir gracias: to kid someone cleverly and with discretion.
It also defines gracioso as both to be handsome and as someone that dice gracias. So the meaning of telling jokes and be funny was also present in the 17th century in Spain. But why? Because you could also have gracia en hablar (to speak gracefully). In 1495 the Spanish-Latin dictionary by Antonio de Nebrija translates gracia en hablar as lepos, oris, lepiditas, atis. Following is the definition of lepor (my bold):
I. pleasantness, agreeableness, attractiveness, charm.
II. In partic.
A. Of behavior, pleasantness, grace, politeness, amiability.
B. As a term of endearment.
C. Of speech, pleasantry, wit, humor.
You can see an example of this usage in the following definition found in a book from the end of the 15th century:
Ridiculum. li. neutro genero el donayre o la que dizen gracia que haze reyr o dar carcajada de risa.
Rodrigo Fernández de Santaella, "Vocabulario eclesiástico", 1499 (Spain).
So someone who spoke cleverly, with wit and humor was also someone graceful, with gracia. As time went by this sense gained strength and besides we just have adopted other words to express that someone is handsome. We still use the word in expressions such as hacer algo con gracia (used in this context maybe as synonym for elegancia), but I would say that we use it mostly in other expressions like tener gracia or ser gracioso to express that someone or something is funny. Other old meanings of gracia have been replaced with favor, as in tener el favor de alguien, hacer un favor, and so on.