There are a few primary causes to this error: Wrong COM port selected (Tools ---> Serial Port), The drivers are not installed properly, or The wrong board is selected (Tools ---> Board ---> Arduino Uno).
What OS are you running?
If the the IDE works occasionally I would expect it to be a driver issue, try this video:Install the Arduino Uno under Windows 8
(if your running windows
(Disclaimer: Not My video)
I'm assuming you've done something like this?:
If you don't see an Arduino device listed under Ports; then you are probably seeing an "Unknown Device". Check to make sure that "Unknown Device" is indeed, the Arduino by plugging and unplugging the board from the computer. "Unknown Device" should appear and reappear. Assuming this is the case, you will need to install the drivers manually for your Arduino. Windows XP, Vista and 7 users may continue with the guide; if you are a Windows8 user, then please make reference to the youtube link above.
Right click on that "Unknown Device" and select "Properties". Click on the "Driver" Tab (Picture 2; Note: In my Picture 2, I already have the driver set, hence why my window reads "Arduino UNO R3 (COM4) Properties", when yours will probably read "Unknown Device Properties").
Select to "Update Driver..." and then choose to select the folder containing the driver on your computer. Conveniently, you already have the drivers for the Arduino on your computer when you installed the Arduino IDE. Navigate to within the Arduino files (where your .exe is; it'll probably be in your Downloads if you didn't move it. In the third picture, you can see my pathway to the drivers folder) to the "drivers" folder and choose the folder. The computer should automatically choose to install one of the drivers within the folder you selected. Simply proceed with the installation and that should fix the problem.
(Disclaimer: Not My instructions)