There are many ways to learn. One of them is seeing, understanding and imitating what you’re seeing in the world around you. Another might be intuiting a solution based on past experiences and failures.

Learning is an iterative process. When you fail, you inevitably have to reassess your goal(s) and the steps you’ve taken to get there. You’ll have to carefully analyse your error to understand how to improve in the next iteration.

You eventually build enough experience that the margin for error gets ever smaller. Depending on the complexity of your task, at some point you might reach your end goal simply by trying.