Please help me through this example with a filter to understand how they work

To fully understand how WP works, you need to understand that WP is an event-driven platform(Though it’s a loose term, but I think it does help to understand better). You need to think like which event is occurring right now.

In this case, a password is generated.

Next question is, How can you change this generated your desired way. WP has provided a filter hook named random_password with apply_filters and also have provided the generated password for you to change( There’s also action hook. See Difference Between Filter and Action Hooks?).

You define the function to use to change the password with add_filter.

I ask you first to read the documentation of

Let’s go back to your example. I also invite you to read the documentation of

codex says

It generates a random password drawn from the defined set of
characters.

Now say, you want to create a plugin which will change the way the password is generated.

You can’t(won’t any way want to) change the code of the WP core files. But you need to accomplish this. WordPress has given you the opportunity to change it via filter hook.

apply_filter function creates a filter in this case named random_password. So, now you have the option to change the generated password as the password is also sent as the argument.

So, random_password is not a function, it’s a filter by which you can filter WP generated password your desired way.

Now going back to your wish of changing the auto generated password, you use the filter like this:

add_filter('random_password', 'my_generated_password');

In this case, every event of password generation/call of wp_generate_password will also call my_generated_password function that you define and it will pass $password as argument.

For demonstration purpose, let’s say you want to add an extra character L to the generated password. In that case, you will define the function like this:

function my_generated_password($password){
    return ($password . 'L');
}