How to display to visitor which site they came from?

WordPress has a function that can extract referrer information (website where user originated from): wp_get_referer().

It will return URL from the originating website. If that is not available it will return FALSE.

This is the simple example PHP function that will display welcome message when called, if the referrer is set:

function wse_280729_show_welcome_message() {
  $ref = wp_get_referer();

  if ($ref) {
    echo "Welcome visitor from '$ref'.";
  }
}

How will you use this is up to you, and depends where you want to display this, and will involve some theme modification and calling this function to show the message (if referrer is set).