Минимальный код такой:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>…</title>
</head>
<body>
	<button id="btn-bold">B</button>
	<button id="btn-italic">I</button>
	<div>
		<textarea id="text-editor" rows="5" cols="40">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</textarea>
	</div>
<script>
function insertTag(tagOpen, tagClose) {
	var textEditor = document.getElementById("text-editor"),
		selStart, selEnd, text, range;
	if ("selectionStart" in textEditor) {
		selStart = textEditor.selectionStart;
		selEnd = textEditor.selectionEnd;
		text = textEditor.value;
		textEditor.value = text.slice(0, selStart) + tagOpen + text.slice(selStart, selEnd) + tagClose + text.slice(selEnd);
	}
	else if ("selection" in document) { //для IE
		textEditor.focus();
		range = document.selection.createRange();
		range.text = tagOpen + range.text + tagClose;
	}
}
document.getElementById("btn-bold").onclick = function () {
	insertTag("<b>", "</b>");
};
document.getElementById("btn-italic").onclick = function () {
	insertTag("<i>", "</i>");
};
</script>
</body>
</html>