It’s analogous to Gerunds and Infinitives in English.
In some circumstances either is ok, in some one is preferred, and in an other only one is acceptable.
Eg.
I like swimming. (Correct)
I like to swim. (Correct)
Swimming is fun. (Preferred)
To swim is fun. (Dispreferred)
I enjoy swimming. (Correct)
I enjoy to swim. (Incorrect)
There are a lot of words about why, but as @Jose7822 said you just have to spend time noticing and learning the most common patterns or set phrases.
Like how it’s:
~たことがある。
and never:
~たのがある。
Editor note
Technically I am comparing gerunds to the infinitive and not participles. The comparison is still useful but wanted to clarify.
Gerund is a verbal in the ~ing form acting as a noun. And the Present participle is the verbal also in ~ing but acting as an adjective. It’s lining verbs that make this strange because in SVC sentences the C(complement) can be an adjective that looks like the O in an SVO sentence.
I am tall. SVC (because tall is an adjective not a noun.
I am swimming (technically this is progressive tense but it’s also just using the participle as an adjective)
Swimming is fun. (Swimming is a gerund and acting as a noun.)
I like swimming. (Swimming is the object of like so still a noun)
The children swimming in the pool are happy. (Swimming is a participle acting as the head of an adjective phrase modifying children.)
You can need to swim but not have “to swim”, but rather have swimming. (One of the many rules you can need an infinitive but you possess a gerund. Infinitive imply will or volition, whereas gerunds represent concepts or the general idea.)
English grammar is wack. Even I made this mistake just now after teaching it to my Japanese students haha.
What I meant to be illustrating is the difference between the Gerund and the Infinitive.