Okay, so for introduction the 4 levels of protein structure (each level influences the levels after it):
- primary (1st): the order of amino acids.
- secondary (2nd): alpha-helicies and beta-sheets
- tertiary (3rd): complex 3d structure
- quaternary (4th) : 3rd+ non-protein elements (ions, co-factors etc)
and / or multple subunits interact. Not every protein has this kind
I think the first does not need any explanation.
The secondary structure is where amino acids form 2D structures: alpha-helices or beta sheets. From wikipedia on a-helix and b-sheet
The alpha helix (α-helix) is a common secondary structure of proteins and is a righthand-coiled or spiral conformation (helix) in which every backbone N-H group donates a hydrogen bond to the backbone C=O group of the amino acid four residues earlier (i+4 \rightarrow i hydrogen bonding)
The β sheet (also β-pleated sheet) is the second form of regular secondary structure in proteins. It is less common than the alpha helix. Beta sheets consist of beta strands connected laterally by at least two or three backbone hydrogen bonds, forming a generally twisted, pleated sheet.
Pictures are from here, that is a great site that explains basic protein strucutre in a quite understandable way.
These secondary structures are important because they can from motifs with specific functions like DNA binding, interaction surface with other proteins. The aforementioned wiki sites have detailed info. These secondary structures and motifs serve as building block for higher order functions. Between motifs "loose" non-structured parts can be found to allow for flexibility.
This is the level where complex 3D geometric structure is defined. The aforementioned 2D structures are packed into domains - a single functional element of a protein. A single protein can have multiple domains and the relative position of these domains result in the tertiary structure. The domains themselves are connected with loose regions that do not show any high order structure and provide flexibility between the domains. Flexibility is necessary for conformational changes.
The quaternary structure is where non protein elements and / or multple subunits are involved. For example the positioning of the heme group in hemoglobin. Or when monomers from dimers, trimers etc. so in general when mature proteins get together to form a larger complex.
Here is a great image about the different structure levels:
So now onto the "how" part :)
As I mentioned in the beginning all levels influence the levels after it. So the amino acid sequence predestines the 2D structures to be formed. The secondary structures themselves are formed by amino acid forming hydrogen bonds with each other. This process occurs "naturrally", so by the inherent properties of the amino acids. At the third level disulfide bonds stabilize the structure. This is also a naturally occuring event. Also binding ions, co-factors, etc also stabilizes the protein strucutre.
The folding process can also be supported by chaperon proteins, These proteins can actively "twist" the polypetide chain using ATP as energy source. They can "sense" if there are hidrophobic amino acid chains facing the outer surface of the protein, a thing usually avoided because these amino acids need to face inner surface of the protein where they can avoid water - this is energetically better. This signal (outward facing hidofobic AAs) indicate that either the protein is un/misfolded or it is damaged because of for example heat - that denatures ("unwinds") proteins.
Finally the wiki page on protein folding if you need more. I hope this helps.