The streams work on a streambuf object. The streambuf is an abstract base class that provides a buffer interface for use by the stream objects (like ostream and istream). Two derived class are provided by the standard, the filebuf and stringbuf classes (where, obviously, the filebuf reads/writes to a file and the stringbuf reads/writes to a string (or character array)).
To associate a stream with a given streambuf object, you can use the rdbuf() function.
If you want to actually create your own streambuf derived class, then I don't really know how to do that, it is probably pretty hard. But usually, either the filebuf or stringbuf should be sufficient for a given application, for instance, most OSes implement things like COM-ports, internet sockets, or console/terminal IO as files (things like look like files anyways, pseudo-files if you want).