I am a beginner in English. I read this sentence in an example: "If you work at a job you enjoy, you will probably do your job better than if you work at a job only to earn money.". Why do we use "to earn" and not "earn"?
Generally, the question of whether to use the infinitive with "to" or the infinitive without "to" depends on the particular word (verb, adjective, noun) which commands the phrase, and you just have to learn that.
For example, modals, such as should, must, take an infinitive without "to", while verbs like ought, have (to) require the "to", even though they mean pretty much the same:
I should go = I ought to go
I must go = I have to go.
But your example is different: this is the use of the "to"-infinitive to express purpose; "work ... to earn money" means "work ... in order to earn money", or "work ... for the purpose of earning money".
Your example wouldn't be grammatical without the "to", because there would be no grammatical structure for the "earn" to fit into.
"to earn" The infinitive form of a verb (in this case earn) is usually preceded by "to"
To preposition (INFINITIVE) The word to is not a preposition. It is often called the sign of the infinitive.
An infinitive is a non-finite verb. In other words, it cannot be the main verb in a sentence.
An infinitive can be used as a noun, an adjective or an adverb. used before a verb showing that it is in the infinitive: The infinitive form of a verb is the verb in its basic form. It is the version of the verb which will appear in the dictionary. enter link description here
If you work at a job you enjoy
This is actually two phrases. There's a that in here that's omitted.
If you work at a job that you enjoy.
So enjoy here is not an infinitive - it has a subject (you).
if you work at a job only to earn money
In this phrase, there's already a subject (you) and verb (work).
Earn doesn't have a subject because
only is not a word that starts a new clause like that (it can start new phrases though)
only is an adverb, meaning it modifies or starts phrases that modify verbs (so it doesn't appear unless a verb is already in the sentence).
it's part of a phrase answering "why do you work" and is meant abstractly.
Since earn is not "finited" by a subject, it's an infintive.
Therefore you put a to in front of it.