What aperture to use depending on the subject?

Aperture is one of those variables beginners usually tend to use in the same way: if they want to have a portrait, they tend to open the aperture as wide as possible (f/1.8 or even f/1.4) to make the background as shallow as possible. On the other hand, in landscape photography, they tend to close the aperture as much as possible (up to f/22) to get the most depth of field as they can. But the truth is that the issue about the aperture is not simple like that, and there are better ways to achieve desired results. That’s why I’d like to discuss a bit about what aperture to use, depending on the situation.