@Renan's answer about using pulsars is excellent, assuming they can be observed. If the spacecraft cannot observe them, which could happen if the craft is in a molecular cloud...
... or if it was attacked with sand, or flew through other fine dust or debris, damaging external sensors, it may not be able to observe enough pulsars, or any, to get oriented. If that is the case, the solution is a technique called "dead reckoning" using an inertial navigation system.
A combination of three gyroscopes strapped to the structure of the craft can be used to reorient it in the same direction it was headed before. Orient the craft so that the gyroscopes are oriented the way they were before losing control, and the craft will be pointing in the same direction it was originally. Note that this does not mean it will be pointing at the same location it was before; the craft will be oriented parallel to its original vector.
In order to figure out how far the craft was displaced from its last known location, a set of three accelerometers can be used. This isn't as "simple" as just reorienting the craft's direction using the gyroscopes; the output of the accelerometers will need to be captured continuously, and calculus used to determine the path taken during the loss-of-control period. If the craft underwent acceleration a similar procedure can be used with the gyroscopes (capture data continuously, use computer) to get the correct orientation. In the case of the angular change, an integral is required, and in the case of position, a double integral is used. (The second integral of acceleration is position). The figure below shows the data flow. The integrals might look kind of scary, but they basically mean "add up all the changes that happened over a given period of time". You can imagine that the integral sign is a childrens' slide, and if you want to integrate, say, the flow of sand, you pour sand onto the top of the slide however you like. It slides down the slide-shaped integral, and accumulates in a bucket at the bottom. When you are done, the sand in the bucket is the accumulation--the integral--of the continuous rate of sand you were pouring, from start to finish, no matter how it changed.
One other note about the figure: if the force of gravity is negligible, as it would be in deep space, you can ignore the gravity box.
Figure 1.4 drawn from [Manon Kok, Jeroen D. Hol and Thomas B. Schon (2017), "Using Inertial Sensors for Position and
Orientation Estimation", Foundations and Trends in Signal Processing: Vol. 11: No. 1-2, pp 1-153.]
(http://dx.doi.org/10.1561/2000000094); Updated version available on arXive
Accelerometers, gyroscopes, and computers might sound delicate or expensive, but small, solid state gyroscopes and accelerometers are already available.
(source: xsens.com)
And in fact, there is at least one chip that integrates three gyroscopes, three accelerometers, and a high-precision digital clock on a chip smaller than a US penny. Here is a DARPA prototype from 2013.
The computation required is easily handled by extremely inexpensive microprocessors.
All of these techniques are subject to variance and drift, so it is unwise to depend on them solely, or for long periods of time, since they need occasional recalibration (from, say, looking at pulsars). However, if it was my spacecraft, I'd have an inertial system like this for backup, as well as a pulsar-spotting system, with plenty of extra accelerometers and gyroscopes.