When you write HTML it is presented to the user exactly as you write it, unless some other code causes it to change. So when you write this:
<p id="demo">Hey</p>
It generates a paragraph tag which displays the word "Hey".
WHen you write Javascript, it does nothing unless something specifically happens to execute that code. So when you write this:
var elem = document.getElementById("demo");
elem.innerHTML = "Hello World!";
it will change the inner text, but only if the code is in a function that gets executed.
Since the text doesn't change, the Javascript is not being executed. See here:
Tryit Editor v3.5[
^] and it will change when you click the button, because teh JS code is inside a click handler function.