So the code that is directly in the head and body of the web page and not enclosed in functions or objects will run sequentially as soon as the file containing the code has loaded sufficiently for that code to be accessed. Basically this means that whatever code you have in the head and body of your page that is not inside a function or object will run as the page is loading once the page has loaded sufficiently for the line of code to be available to be run. What this means is that any code placed directly in the page that is intended to interact with elements within the page needs to appear further down the page than the elements in the page that it needs to interact with. In general this means that if you want to use direct code to interact with your page content that such code should be placed at the bottom of the body.
Code that is within functions and objects will be run whenever that function or object is called. If it is called from code that is directly in the head or body of the page then its place in the execution order is effectively the spot where the function or object is called from the direct code.
Note that assigning a function to an event handler or listener does not result in the function being run at the point where it is assigned provided that you are actually assigning the function itself and not running the function and assigning the value returned (which would need to be another function). That's why you generally do not see the () on the end of the function name when it is being assigned to an event since the addition of the parentheses to the end would run the function and assign the value returned rather than assigning the function itself.
Functions that are attached to event handlers and listeners will be run when the event that they are attached to is triggered. Most of the events are actually triggered by the visitor to your page interacting with the page but there are a few exceptions such as the load event on the window itself which is triggered when the page finishes loading.
Any functions that are attached to events on elements within the page itself will be dependent on each individual visitor as to when and even if the associated code will be run. The code will only run when the particular event actually occurs to trigger the running of that code. Since that code would generally interact with what your visitor is doing that results in that event being triggered it shouldn't matter if the code never runs for a given visitor since that visitor has obviously not performed the interaction that requires it.