You’ve hooked into a filter. The code will run when that filter is executed. In your case, that is when the_search_query()
function runs:
2595 /**
2596 * Display the contents of the search query variable.
2597 *
2598 * The search query string is passed through {@link esc_attr()}
2599 * to ensure that it is safe for placing in an html attribute.
2600 *
2601 * @since 2.1.0
2602 */
2603 function the_search_query() {
2604 /**
2605 * Filter the contents of the search query variable for display.
2606 *
2607 * @since 2.3.0
2608 *
2609 * @param mixed $search Contents of the search query variable.
2610 */
2611 echo esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) );
2612 }
WordPress core only runs that function a few times (you can grep
your source to find it) but themes and plugins may run it as well.