The two Books of Samuel are part of the Deuteronomistic history that constitutes a theological history of the Israelites which explains God’s law for Israel under the guidance of the prophets.  According to tradition, 2 Samuel was written by Samuel, with additions by the prophets Gad and Nathan.  It is believed to have been compiled around 600 BC.  Up until around 200 BC, 1 Samuel and 2 Samuel were a single book.