StackOverflow is merely moving the selected lines over using 4 spaces when you select lines within the <textarea> element, which then is used as markup that can be detected via JavaScript. It would look something like the following:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<textarea id="myTextarea" style="width:500px;height:400px;"></textarea>
<button id="clickme">Click me!</button>
<script>
    $('#clickme').on('click', function() {
        var value = document.getElementById("myTextarea").value.replace(/(\n|^)[ ]{4}([^\n]*)/g, function(c) {
            // console.log('found! c ==', c, '; arguments ==', arguments);
            var lineData = arguments[2];
            return "<pre><code>" + lineData.replace(/</g, '<').replace(/>/g, '>') + "</code></pre>";
        });
        // console.log('value ==', value);
        $('body').append(value);
    });
</script>
In the <textarea> place text such as the following, making sure that 4 spaces lead before each line that needs to be formatted as displayable HTML:
<div>
    Hello world!<br />
</div>
Then finally click the "Click me!" button. jQuery was used in this code example for brevity.